草庐IT

unique_future

全部标签

c++ - shared_ptr, unique_ptr, ownership,在这种特殊情况下我是不是做得太过分了?

我在图形应用程序上工作,并且一直在使用共享指针和唯一指针,主要是因为它为我处理内存重新分配(又名方便),这可能很糟糕(如果这就是我使用它们的原因)。我最近在Stackoverflow上阅读了一个问题的答案,其中提到根据B.Stroustrup的说法,通常不应使用唯一/共享指针,而应改为按值传递参数。我有一个图形方面的案例,我认为使用shared_ptr是有意义的,但我想从专家那里知道(我不是C++专家)我是否在做/想它,如果是的话他们会做什么(为了符合C++推荐和效率)我将以渲染/光线追踪中出现的一般问题为例。在这个特定的问题中,我们有一个对象池(我们将使用三角形来进行解释)和一个结构

c++ - 我们如何从成员函数返回一个 unique_pointer 成员?

我有一个带有指针成员的基类。我必须做出有根据的猜测来确定它应该是unique_ptr还是shared_ptr。它们似乎都无法解决我的特定用例。classBase{public:Base():pInt(std::unique_ptr(newint(10))){};virtualstd::unique_ptrget()=0;//Base():pInt(std::shared_ptr(newint(10))){};//Alternateimplementation//virtualstd::shared_ptrget()=0;//Alternateimplementationprivate:

c++ - 有效 future 与默认构建 future

我正在并发编程课上学习futures。我的教授在她的幻灯片中说明了这一点:"Valid"futuresare future objectsassociatedtoa sharedstate,andareconstructedbycallingoneofthefollowingfunctions:asyncpromise::get_futurepackaged_task::get_futurefuture objectsareonlyusefulwhentheyare valid. Default-constructed future objectsarenot valid (unles

c++ - RAII、unique_ptr 和输出参数

我是一名尝试学习C++11的C#开发人员。我正在尝试使用windns.h查询DNS。我从DnsQuery()开始,读到我需要使用DnsRecordListFree()释放结果记录输出参数。C#方法可能是使用try-finallyblock来确保我无论如何都释放资源。但我了解到没有finallyblock,并且windns.h确实应该与时俱进并实现RAII兼容接口(interface)(据我了解典型的建议)。我没有等待它发生,而是尝试制作一个RAII包装器类,其析构函数调用DnsRecordListFree()并使用运算符重载转换来获取原始指针。但我对如何正确使用此句柄或指针来获取输出参

c++ - 如何将异常报告给 boost::future?

如果我使用Boostfutures,并且future报告true到has_exception(),是否有任何方法可以检索该异常?例如,这里是下面的代码:intdo_something(){...throwsome_exception();...}...boost::packaged_tasktask(do_something);boost::unique_futurefi=task.get_future();boost::threadthread(boost::move(task));fi.wait();if(fi.has_exception()){boost::rethrow_exc

c++ - 具有 unique_ptr 的用户友好型 API

我正在为OpenGL实现一个简单的GUI,主要是作为我自己的练习。这个想法是有一个Gui类,其中每个实例都可以分配给不同的渲染目标(例如后台缓冲区或纹理)。GUI元素(小部件)恰好分配给Gui类的一个实例。我想在GUI中存储元素是unique_ptr的典型用例.这是我想出的:classElement{public:Element();virtual~Element();staticstd::unique_ptrcreate_unique();};classGui{public:typedefstd::unique_ptrelement_ptr;Gui();voidaddElement(

c++ std::unique_ptr 不会在 map 中编译

我目前正在尝试将std::unique_ptr存储在std::unordered_map中,但出现奇怪的编译错误。相关代码:#pragmaonce#include"Entity.h"#include#includeclassEntityManager{private:typedefstd::unique_ptrEntityPtr;typedefstd::mapEntityMap;EntityMapmap;public:/*AddsanEntity*/voidaddEntity(EntityPtr);/*RemovesanEntitybyitsID*/voidremoveEntity(i

c++ - 如何编写指针和数据为 const 的 unique_ptr

我第一次尝试编写一个基于范围的for循环来遍历unique_ptr时,我写道:std::vector>vec;//Initializevecfor(autov:vec)//error{}然后我意识到这是在尝试创建每个元素的拷贝,这对unique_ptr没有意义。所以后来我写了它作为引用:for(auto&v:vec){}在它前面添加一个const可以防止我更改指针。for(constauto&v:vec){v=nullptr;//error(good!)}要怎么写,才能让指向的数据不能改变?例如,以下代码不应编译。for(???v:vec){v->func();}classFoo{pu

c++ - std::unique_ptr 和 std::shared_ptr 之间破坏行为差异的基本原理是什么?

来自http://en.cppreference.com/w/cpp/memory/unique_ptr:IfTisderivedclass(sic)ofsomebaseB,thenstd::unique_ptrisimplicitlyconvertibletostd::unique_ptr.Thedefaultdeleteroftheresultingstd::unique_ptrwilluseoperatordeleteforB,leadingtoundefinedbehaviorunlessthedestructorofBisvirtual.Notethatstd::shared

c++ - unique_ptr、自定义删除器和零规则

我正在编写一个类,该类使用两个通过C接口(interface)创建的对象。对象看起来像:typedefstruct...foo_t;foo_t*create_foo(int,double,whatever);voiddelete_foo(foo_t*);(与bar_t类似)。因为C++11,我想将它们包装在一个智能指针中,这样我就不必编写任何特殊方法。该类将拥有这两个对象的唯一所有权,因此unique_ptr在逻辑上是有意义的......但我仍然必须编写一个构造函数:templateusingunique_ptr_deleter=std::unique_ptr;structMyClas