草庐IT

together_unique

全部标签

c++ - 如何将字符串的值赋给 std::unique_ptr<std::string>?

声明std::unique_ptr之后但没有分配它(因此它包含一个std::nullptr开始)-如何为其分配一个值(即我不再希望它持有std::nullptr)?我尝试过的两种方法都不起作用。std::unique_ptrmy_str_ptr;my_str_ptr=newstd::string(another_str_var);//compilererror*my_str_ptr=another_str_var;//runtimeerror哪里another_str_var是一个std::string这是较早声明和分配的。清楚我的理解是什么std::unique_ptr正在做的是严重

c++ - 何时用单例替换全局 std::unique_ptr

一位同事坚持对所有全局指针变量使用Meyer的Singleton,因为“不能保证全局unique_ptr的构造不会抛出”。所以不是:#includestd::unique_ptrptr(nullptr);//Apparentlythisisn'tsafe.intmain(/*blah*/){ptr.reset(newFoo());}我们现在有unique_ptrsingleton{try{staticunique_ptrptr();returnptr;}catch(...){std::cerr();}intmain(){}对我来说,这似乎是一种寻找问题的解决方案。他有道理吗?

c++ - std::make_unique 可以与抽象接口(interface)一起使用吗?

考虑以下代码行:autosource1=std::unique_ptr(newGpsDevice(comPort,baudrate));autosource2=std::unique_ptr(newGpsLog(filename));如何使用VS2013支持的新std::make_unique函数来编写?有可能吗?**我的问题是我不知道如何告诉std::make_unique要实例化哪种对象。因为只有构造函数的参数被传入,所以似乎无法控制它。 最佳答案 std::unique_ptrbase_ptr=std::make_unique

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