草庐IT

unique_future

全部标签

c++ - 如何统一初始化 unique_ptr 的映射?

我有这段代码可以将映射从into初始化为unique_ptr。autoa=unique_ptr(newA());map>m;m[1]=move(a);我可以使用统一初始化吗?我试过了map>m{{1,unique_ptr(newA())}};但是我得到了一个错误。错误信息的一部分是Ininstantiationof'std::_Rb_tree_node::_Rb_tree_node(_Args&&...)[with_Args={conststd::pair>>&};_Val=std::pair>]':...Infileincludedfrom/opt/local/include/gcc

c++ - 为什么我不能从一对中返回一个 unique_ptr?

为什么我不能从一对中返回一个unique_ptr?#include#include#includeusingnamespacestd;unique_ptrget_value(){pair,int>p(unique_ptr(newint(3)),4);returnp.first;}intmain(void){cout当我尝试使用g++4.6编译它时,我得到:../main.cpp:Infunction‘std::unique_ptrget_value()’:../main.cpp:9:11:error:useofdeletedfunction‘std::unique_ptr::uniqu

c++ - 奇怪的错误:在没有真正创建指针时使用已删除的函数 'std::unique_ptr<_Tp, _Dp>::unique_ptr

我有一个类,看起来像这样:templateusingVectorPtr=std::vector>;templateusingVectorRawPtr=std::vector;classItemsSet{//&items);~ItemsSet()=default;VectorRawPtrGetItems();VectorRawPtrGetSuitableItemsForPeriod(constIPeriod&period);doubleCalculateTotal();private:VectorPtr_items;};构造函数看起来像:ItemsSet::ItemsSet(Vector

c++ - Boost 无法从演示中找到 future::then

那时我想尝试boostfuture,我安装了boost1.55并包含在make文件中,我想尝试官方演示#defineBOOST_THREAD_PROVIDES_FUTURE#includeusingnamespaceboost;intmain(){futuref1=async([](){return123;});futuref2=f1.then([](futuref){returnf.get();});//here.get()won'tblock});}但是编译的时候总是报错error:‘classboost::future’hasnomembernamed‘then’当我用f2注释行

c++ - vector<unique_ptr> 的唯一拷贝

我有一个包含vector的类对象.我想要这个对象的拷贝来运行非常量函数。原始拷贝必须保持const。这样一个类的复制构造函数是什么样的?classFoo{public:Foo(constFoo&other):???{}std::vectorptrs;}; 最佳答案 您不能简单地复制std::vector因为std::unique_ptr不可复制,因此它将删除vector复制构造函数。如果您不更改存储在vector中的类型,那么您可以通过创建一个全新的vector来进行“复制”std::vector>from;//thishasthe

c++ - 为什么 unique_ptr 有一个 nullptr_t 构造函数?

我不清楚有什么好处。如果我有:Foo*foo=nullptr;std::unique_ptrunique_foo(foo);在那种情况下是否调用了nullptr_t构造函数?或者仅当您这样做时:std::unique_ptrunique_foo(nullptr);谢谢!有一些讨论here这是为了允许你传入nullptr_t,否则它不会编译,因为它不会转换为类型指针。所以我的问题可能是为什么它不转换? 最佳答案 一个可能的原因是采用unique_ptr::pointer参数的unique_ptr构造函数是显式。这意味着在没有uniqu

c++ - std::unique_ptr 是 RAII 的应用吗?

这是对它的准确描述吗?有道理吗?您是否保证在unique_ptr超出范围之前它指向的对象不会被删除[即使您没有使用unique_ptr]? 最佳答案 是的,std::unique_ptr遵循RAII设计原则。不,std::unique_ptr不会阻止其他代码做一些愚蠢的事情,比如在属于unique_ptr的指针上调用delete>。unique_ptr本身将在其拥有的对象上调用deleter1,当出现以下任一情况时:超出范围或unique_ptr被重新分配(通过operator=或reset)以指向不同的对象还可以通过移动到不同的智

c++ - 为什么我会收到编译错误 "use of deleted function ' std::unique_ptr ...”

我收到一个巨大的编译错误信息c:\mingw\include\c++\6.1.0\bits\predefined_ops.h:123:18:error:useofdeletedfunction'std::unique_ptr::unique_ptr(conststd::unique_ptr&)[with_Tp=Deduction;_Dp=std::default_delete]'{returnbool(_M_comp(*__it1,*__it2));}当我将自定义比较器传递给STLset_difference函数时。我的代码:structValue{std::stringded_cod

c++ - 将 std::unique_ptr 转换为 std::unique_ptr 到父类(super class)的正确方法是什么?

假设我有一个名为foo的类它继承自一个名为bar的类.我有一个std::unique_ptr到foo的实例我想将它传递给一个只接受std::unique_ptr的函数.我怎样才能转换指针以便它在我的函数中工作? 最佳答案 您可以转换std::unique_ptr右值std::unique_ptr:std::unique_ptrf(newfoo);std::unique_ptrb(std::move(f));显然,指针将由b拥有如果b被摧毁bar需要有一个virtual析构函数。 关于c+

c++ - std::unique_ptr<T[]>::reset 在 gcc 6 中的实现

从C++中的GCC6开始,unique_ptr::reset的声明/定义方法(不是那个,只接受nullptr_t)看起来像这样:template,__and_,is_pointer,is_convertible::type(*)[],element_type(*)[]>>>>>voidreset(_Up__p)noexcept{usingstd::swap;swap(std::get(_M_t),__p);if(__p!=nullptr)get_deleter()(__p);}这在某些时候已更改以实现N4089.根据该文件:Thisfunctionbehavesthesameasthe