草庐IT

together_unique

全部标签

c++ - 带有大括号初始化的 make_unique

https://en.cppreference.com/w/cpp/memory/unique_ptr/make_unique写道std::make_unique可以实现为templatestd::unique_ptrmake_unique(Args&&...args){returnstd::unique_ptr(newT(std::forward(args)...));}这不适用于没有构造函数的普通结构。这些可以用大括号初始化,但没有非默认构造函数。示例:#includestructpoint{intx,z;};intmain(){std::make_unique(1,2);}Com

c++ - "error: use of deleted function"在 move 构造函数中对 unique_ptr 调用 std::move 时

#includeclassA{public:A(){}A(constA&&rhs){a=std::move(rhs.a);}private:std::unique_ptra;};此代码无法使用g++4.8.4编译并抛出以下错误:error:useofdeletedfunction‘std::unique_ptr&std::unique_ptr::operator=(conststd::unique_ptr&)[with_Tp=int;_Dp=std::default_delete]’a=std::move(rhs.a);^我知道unique_ptr的复制构造函数和复制赋值构造函数已删除

c++ - 从函数返回的 unique_ptr 的范围是什么?

这能正常工作吗?(见示例)unique_ptrsource(){returnunique_ptr(newA);}voiddoSomething(A&a){//...}voidtest(){doSomething(*source().get());//unsafe?//Whendoesthereturnedunique_ptrgooutofscope?} 最佳答案 从函数返回的unique_ptr没有范围,因为范围只适用于名称。在您的示例中,临时unique_ptr的生命周期以分号结束。(所以是的,它会正常工作。)一般来说,当完整表达

c++ - 为什么 make_unique 不能与 unique_ptr::reset 一起使用?

我尝试用VS2013编译一些C++代码,unique_ptr::reset()似乎不适用于make_unique();一个小的可编译重现代码片段如下:#includeusingnamespacestd;intmain(){unique_ptrp=make_unique(3);p.reset(make_unique(10));}从命令行编译:C:\Temp\CppTests>cl/EHsc/W4/nologotest.cpp这些是来自MSVC编译器的错误:test.cpp(6):errorC2280:'voidstd::unique_ptr>::reset>>(_Ptr2)':attem

c++ - 为什么 std::unique_ptr 不隐式转换为 T* 和 const T*?

如果我自己写,我想我会这样做:template>classUptr:privateDtor{T*vl_;public:explicitUptr(T*vl=nullptr)noexcept:vl_(vl){}~Uptr()noexcept{Dtor::operator()(vl_);}Uptr&swap(Uptr&o)noexcept{T*tmp;tmp=vl_;vl_=o.vl_;o.vl_=tmp;}Uptr&operator=(Uptr&&o)noexcept{o.swap(*this);}Uptr&operator=(nullptr_t)noexcept{vl_=nullptr;

c++ - std::unique_ptr 作为在 std::thread 中运行的参数

这个问题在这里已经有了答案:visualstudioimplementationof"movesemantics"and"rvaluereference"(2个答案)关闭7年前。所以我试图将std::unique_ptr作为参数传递给在单独线程中启动的函数,并且我在编译时遇到了一个奇怪的错误:1>c:\programfiles(x86)\microsoftvisualstudio12.0\vc\include\functional(1149):errorC2280:'std::unique_ptr>::unique_ptr(conststd::unique_ptr>&)':attemp

c++ - std::unique_ptr 是基础对象的两倍大

我遇到了一个问题(特别是MSFTVS10.0的实现)std::unique_ptrs。当我创建它们的std::list时,我使用的内存是创建仅底层对象的std::list时的两倍(注意:这是一个大对象——~200字节,所以它不仅仅是一个周围有额外的引用计数器)。换句话说,如果我运行:std::listX;X.resize(1000,MyObj());我的应用程序需要的内存是我运行时的一半:std::list>X;for(inti=0;i(newMyObj()));我检查了MSFT的实现,但没有发现任何明显的问题——有人遇到过这个问题并有任何想法吗?编辑:好的,更清楚/具体一点。这显然是

c++ - 为什么 std::unique_ptr 没有 operator<<?

C++11标准中的[util.smartptr.shared.io]要求operator为了shared_ptr小号:templatebasic_ostream&operator&os,shared_ptrconst&p);然而,除非我遗漏了它,否则我在[unique.ptr]中看不到任何类似的东西,而且引用en.cppreference.com同意。有没有理由区别? 最佳答案 Isthereareasonforthedifference?不,没有。与make_unique一样,这是一个“疏忽”,应该在将来添加(如果有人愿意发送提案

c++ - std::unique_ptr<T[]> 和自定义分配器删除器

我正在尝试使用std::unique_ptr使用自定义内存分配器。基本上,我有自定义分配器,它们是IAllocator的子类,它提供了以下方法:void*Alloc(size_tsize)templateT*AllocArray(size_tcount)voidFree(void*mem)templatevoidFreeArray(T*arr,size_tcount)由于底层内存可能来自预分配block,因此我需要特殊的...Array()-分配和释放数组的方法,它们分配/释放内存并调用T()/~T()在范围内的每个元素上。现在,据我所知,std::unique_ptr的自定义删除器|

c++ - `std::vector< std::unique_ptr< T >>` 错误

我看到一些错误传递std::vector>周围有std::move.重现问题的代码是这样的:#include//forstd::unique_ptr#include//forstd::move#include//forstd::vectorstructbar{};usingvtype=std::vector>;structfoo{foo(vtypev):_v(std::move(v)){}private:vtype_v;};vtypegetVector(){return{std::move(std::unique_ptr(newbar()))};};intmain(){foof(std