草庐IT

unique_future

全部标签

c++ - 线程安全的 unique_ptr move

是否可以使用C++11原子操作安全地moveunique_ptr?目前我有这样的代码std::unique_ptrDataManager::borrowSyncToken(){std::unique_locksyncTokenLock(syncTokenMutex);returnstd::move(syncToken);}我想知道是否有更优雅的方式,比如简单地声明:std::atomic>syncToken;并避免互斥量的需要。或者我可能根本不需要关心这里的锁并且std::move已经是原子的了?经过到目前为止的研究,在我看来:std::move本身不是原子的,需要有一些同步,否则2个

c++ - 如何使具有 unique_ptr 成员的类与 std::move 和 std::swap 一起工作?

我有课。它有一个unique_ptr成员。classA{std::unique_ptrm;};我希望它适用于以下语句Aa;Ab;a=std::move(b);std::swap(a,b);如何制作?根据评论,我有一个问题。这个编译器依赖吗?如果我什么都不做,它无法通过VC++2012的编译。我试过structA{A(){}A(A&&a){mb=a.mb;ma=std::move(a.ma);}A&operator=(A&&a){mb=a.mb;ma=std::move(a.ma);return*this;}unique_ptrma;intmb;};但不确定这是否是最好和最简单的方法。

c++ - 如何避免 VS 中 unique_ptr 检查的性能警告?

这段代码:unique_ptra;if(a){cout甚至这段代码:unique_ptra;if(static_cast(a)){cout导致此警告:warningC4800:'void(__cdecl*)(std::_Bool_struct&)':forcingvaluetobool'true'or'false'(performancewarning)with[_Ty=std::unique_ptr]在VisualStudio2012中,警告级别为3。在第一条评论之后,我发现它只有在公共(public)语言运行时支持/clr被打开时才会发生。我应该如何避免它?if(a.get()!=

c++ - unique_ptr 指向指针的删除器指针

我正在尝试使用unique_ptr自定义删除器映射第三方API。问题是API是这样的:x*x_alloc_x(void);voidx_free_x(x**p);API要我提供一个指向它的指针的指针,以便将其设置为NULL。我一直在编写我的deleter仿函数作为对指针的引用,我使用“&”运算符将其转换为指针对指针。structXDeleter{voidoperator(x*&p){x_free_x(&p);}};这适用于GCC4.6,但标准实际上允许这样做吗?如果没有,是否有符合标准的方法将此API映射到删除器? 最佳答案 代码格式

c++ - 如何确保返回 vector <unique_ptr> 的常量

我问了相关问题here.现在它有点微妙。代码如下:classMyClass{public:constvector>&get_const_objs()const;private:vector>m_objs;};我的意图是从get_const_objs()返回的vector是只读的,但问题是因为vector的元素不是常量,所以调用者仍然可以更改单个元素,例如constvector>&objs=pMyClass->get_const_objs();unique_ptrp=move(objs[0]);我的解决方案是向vector中插入常量:constvector>&get_const_objs

c++ - 为什么 unique_ptr 对 auto_ptr 有重载?

我遇到编译器错误并注意到一些有趣的事情。出于某种原因,unique_ptr对auto_ptr有重载,但我认为auto_ptr已被弃用:/usr/local/include/c++/4.9.0/bits/unique_ptr.h:228:2:note:templatestd::unique_ptr::unique_ptr(std::auto_ptr&&)unique_ptr(auto_ptr&&__u)noexcept;/usr/local/include/c++/4.9.0/bits/unique_ptr.h:228:2:note:templateargumentdeduction/s

c++ - 为什么 std::make_unique 等不存在 std::initializer_list 重载?

参见ShouldIuse()or{}whenforwardingarguments?.foo是std::vector克隆。在N4140中,unique.ptr.createstd::make_unique指定为:templateunique_ptrmake_unique(Args&&...args);Remarks:ThisfunctionshallnotparticipateinoverloadresolutionunlessTisnotanarray.Returns:unique_ptr(newT(std::forward(args)...)).这意味着需要实现才能使用()而不是{

c++ - Boost::Future 延迟延续展开死锁

我正在使用Boost的promise和future并在使用延续时遇到了边缘情况。我的代码使用一个返回future的延续,并在获取其值之前解包then()的结果。#defineBOOST_THREAD_VERSION5#include#includeintmain(intargc,char*argv[]){boost::promisepromise;boost::futurefuture=promise.get_future();promise.set_value(42);intresult=future.then(boost::launch::async,[](boost::futur

c++ - 在什么时候将 unique_lock 与 shared_mutex 一起使用?

通常,当使用“普通”互斥量时,您会像在remove1()中那样使用它。但是,现在有了shared_lock和unique_lock,是否应该先使用共享锁,只有在必要时才使用唯一锁?请注意,当模型不存在时,remove()可能不需要unique_lock。voidremove1(intid){std::unique_locklock(mutex_);for(autoit=models_.begin();it!=models_.end();++it)if((*it)->getId()==id){it=models_.erase(it);return;{}voidremove2(intid)

c++ - 如何让这段涉及 unique_ptr 的代码进行编译?

#include#includeusingnamespacestd;classA{public:A():i(newint){}A(Aconst&a)=delete;A(A&&a):i(move(a.i)){}unique_ptri;};classAGroup{public:voidAddA(A&&a){a_.emplace_back(move(a));}vectora_;};intmain(){AGroupag;ag.AddA(A());return0;}不编译...(说unique_ptr的复制构造函数被删除)我尝试用forward替换move。不确定我这样做是否正确,但它对我不起作