草庐IT

unique_future

全部标签

c++ - 使用 std::unique_ptr 的 double (二维)数组

我有一个由指向指针的指针分配的double组。//pointertopointerint**x=newint*[5];//allocationfor(i=0;i我正在尝试使用unique_ptr来做到这一点:std::unique_ptr[]>a(newstd::unique_ptr[5]);for(i=0;i但一直收到错误消息说没有operator=匹配这些操作数。我在这里做错了什么? 最佳答案 您不能分配int*到std::unique_ptr,这就是你的错误的原因。正确的代码是a[i]=std::unique_ptr(newi

c++ - get() 不会破坏 std::unique_ptr 背后的想法吗?

示例代码:#include#includeintmain(){std::unique_ptrintPtr{newint(3)};int*myPtr=intPtr.get();*myPtr=4;std::cout这不会破坏std::unique_ptr背后的全部目的吗?为什么允许这样做?编辑:我认为std::unique_ptr背后的全部目的是拥有一个对象的唯一所有权。但是这个例子,可以通过另一个指针来修改对象。这不会破坏std::unique_ptr的目的吗? 最佳答案 同时Smartpointers管理被指向对象的生命周期,访问底

c++ - unique_ptr 运算符=

std::unique_ptrptr;ptr=newint[3];//errorerrorC2679:binary'=':nooperatorfoundwhichtakesaright-handoperandoftype'int*'(orthereisnoacceptableconversion)为什么不编译?如何将native指针附加到现有的unique_ptr实例? 最佳答案 首先,如果你需要一个独特的数组,就制作它std::unique_ptrptr;//^^^^^这允许智能指针正确使用delete[]来释放指针,并定义ope

c++ - 为什么 unique_ptr 不能推断删除器的类型?

假设我想使用带有unique_ptr的自定义删除器:voidcustom_deleter(int*obj){deleteobj;}为什么我要这样写:std::unique_ptrx(newint,custom_deleter);而不是这个:std::unique_ptrx(newint,custom_deleter);//doesnotcompile?不能推断删除器的类型吗? 最佳答案 对于unique_ptr,删除器是类型的一部分:template>classunique_ptr;因此,当您构造一个对象时,您需要指定它的类型。你正

c++ - 如何在 DDD(或 gdb)中使用 unique_ptr 调试 C++11 代码?

std::unique_ptr很好,但我发现在DDD中调试时不太舒服或gdb.我正在使用作为gcc一部分的gdbpretty-print(例如,/usr/share/gcc-4.8.2/python/libstdcxx/v6/printers.py)。这是可读性的一大胜利,例如:$printpTeststd::unique_ptrcontaining0x2cef0a0但是,取消引用指针不起作用:$print*pTestCouldnotfindoperator*.当我需要访问该值时,我必须手动复制指针并将其转换为正确的类型,例如:print*((MyType*)0x2cef0a0)如果进

c++ - 将 reinterpret_cast 输入重新解释为 std::unique_ptr 永远不会真正安全吗?

当使用具有可变大小结构(必须分配为byte[]然后转换为结构)的各种API时,如果unique_ptr持有者可以指向该结构,那将是很好的,因为这就是我们将要做的正在使用。例子:std::unique_ptrv;v.reset(reinterpret_cast(newBYTE[bytesRequired]));这允许`v提供结构本身的View,这是更可取的,因为我们不需要第二个变量,除了删除之外我们不关心字节指针。问题在于可能会在强制转换上对指针进行thunk(使其释放不安全)。我看不出为什么编译器会在cast上更改指针值(因为没有继承),但我听说标准保留对任何cast上的任何指针进行t

c++ - 使用别名模板时无法将 `std::unique_ptr` 分配给 clang 中的基类

以下代码在gcc4.9.3和clang3.7.1上编译和运行得很好//std::unique_ptr#include//Templateclassfortemplate-templateargumentstemplatestructBar{};//BaseclasstemplateclassXX>structBase{};//DerivedclassthatoperatesonlyonBartemplatestructDerived:publicBase{};//Holdstheunique_ptrtemplateclassXX>structFoo{std::unique_ptr>fo

c++ - std::list<std::future> 析构函数不阻塞

我有一个多线程应用程序,有一个循环等待用户输入作为主线程。在正确的输入上,它应该停止循环并等待所有其他线程正确结束。为此,我创建了一个std::list,其中放置了为创建线程而创建的std::future对象std::list>threads;threads.emplace_front(std::async(std::launch::async,...));我的印象是,让list超出范围应该阻塞,直到所有线程返回它们的main函数,因为list的析构函数将destrurct所有std::future元素和thedestructorofthose将等待线程完成。编辑:因为它是相关的,所以

c++ - 为什么 is_copy_constructible 在 MSVC12 中为 unique_ptr 返回 true

我原以为这个静态断言会触发:#include#includeintmain(){static_assert(std::is_copy_constructible>::value,"UPtrhascopyconstructor?");}但事实并非如此。使用MSVC12编译:Microsoft(R)C/C++OptimizingCompilerVersion18.00.31101forx64 最佳答案 static_assert应该触发,std::unique_ptr有一个隐式删除的复制构造函数,所以这是一个错误。这看起来与此错误报告有

c++ - unique_ptr 的赋值运算符复制由引用存储的删除器。它是功能还是错误?

想象一下当你有一个unique_ptr时的情况使用由引用存储的自定义删除器:structCountingDeleter{voidoperator()(std::string*p){++cntr_;deletep;}unsignedlongcntr_=0;};intmain(){CountingDeleterd1{},d2{};{std::unique_ptrp1(newstd::string{"first"},d1),p2(newstd::string{"second"},d2);p1=std::move(p2);//doesd1=d2undercover}std::cout令我惊讶的