草庐IT

Unique_ptr

全部标签

c++ - 原始指针和weak_ptr有什么区别?

如标题。这个问题可能已经有了答案,但我没有找到答案。 最佳答案 裸指针和weak_ptr之间的基本概念区别在于,如果指向的对象被销毁,裸指针不会告诉你。这称为悬空指针:指向不存在的对象的指针。它们通常很难追踪。weak_ptr会。为了使用weak_ptr,您必须首先将其转换为shared_ptr。如果那个shared_ptr没有指向任何东西,那么这个对象就被删除了。例如:#include#includestd::weak_ptrwp;voidtest(){autospt=wp.lock();//Hastobecopiedintoas

c++ - GCC 错误 : cannot convert 'const shared_ptr<...>' to 'bool' in return

我正在切换到GCC4.6.1,它开始提示在GCC4.4和MSVC10上运行良好的代码。从这样的函数返回时,它似乎不想在shared_ptr和bool之间进行转换:classClass{shared_ptrpointer_;};boolClass::Function()const{returnpointer_;}使用returnstatic_cast(pointer_);一切正常。到底他妈发生了什么?这是--std=cpp0x. 最佳答案 在C++11中,shared_ptr有一个explicitoperatorbool这意味着sha

c++ - 为什么 std::make_unique 而不是 std::unique_ptr::make?

为什么C++采用自由函数:std::make_unique(...);std::make_shared(...);而不是使用静态成员函数:std::unique_ptr::make(...);//staticstd::shared_ptr::make(...);//static? 最佳答案 TL;DR:静态成员函数始终可以访问私有(private)数据,但自由函数只有在明确标记为friend时才能访问私有(private)数据。选择将这些函数实现为自由函数(有一小部分实现为友元函数)不是随机的历史产物,而是一个经过深思熟虑的决定,以

C++ - 返回 const unique_ptr

我想知道为什么编译时会出错:conststd::unique_ptrget(){returnstd::make_unique(10);}intmain(){conststd::unique_ptrvalue=get();returnEXIT_SUCCESS;}我收到以下错误:main.cpp:Infunction‘intmain()’:main.cpp:10:44:error:useofdeletedfunction‘std::unique_ptr::unique_ptr(conststd::unique_ptr&)[with_Tp=int;_Dp=std::default_delet

c++ - 带有 boost shared_ptr 的自定义(池)分配器

我想从一个池中分配由shared_ptr管理的对象,比如Boost的Pool接口(interface),如何实现? 最佳答案 这是执行您想要的操作的代码(可能无法编译,因为我手头没有boost并且我正在从内存中编写它):classYourClass;//yourdatatype,definedsomewhereelseboost::object_poolallocator;voiddestroy(YourClass*pointer){allocator.destroy(pointer);}boost::shared_ptrcreat

c++ - Pimpl - 为什么可以在不完整的类型上调用 make_unique

为什么make_unique调用会编译?make_unqiue不要求它的模板参数是一个完整的类型吗?structF;intmain(){std::make_unique();}structF{};问题源于我的PIMPL实现的“问题”:我确实理解为什么必须在实现类(PIMPL)的cpp文件中由用户声明和定义析构函数。但是移动包含pimpl-的类的构造函数仍然可以编译。classObject{};classCachedObjectFactory{public:CachedObjectFactory();~CachedObjectFactory();std::shared_ptrcreate

c++ - 为什么不允许使用 `make_unique<T[N]>`?

假设命名空间std贯穿始终。C++14委员会草案N3690定义std::make_unique因此:[n3690:20.9.1.4]:unique_ptrcreation   [unique.ptr.create]templateunique_ptrmake_unique(Args&&...args);1Remarks:ThisfunctionshallnotparticipateinoverloadresolutionunlessTisnotanarray.2Returns:unique_ptr(newT(std::forward(args)...)).templateunique_

c++ - 为什么 libc++ 的 shared_ptr 实现使用完整的内存屏障而不是放松的?

在shared_ptr的boost实现中,它使用了relaxedmemoryorderingtoincrementitsreferencecount.这看起来很安全,因为减量使用获取/释放来确保在释放内存之前线程可以看到任何先前的减量。这个方法似乎是正确的,出现在HerbSutterstalkonatomics在libc++的实现中使用fullmemorybarrierstemplateinlineTincrement(T&t)_NOEXCEPT{return__sync_add_and_fetch(&t,1);}templateinlineTdecrement(T&t)_NOEXCE

c++ - 如何在 C++11 中实现 make_unique 函数?

这个问题在这里已经有了答案:make_uniqueandperfectforwarding(6个回答)关闭9年前。我的编译器不支持make_unique。一个怎么写?templateunique_ptrmake_unique(Args&&...args); 最佳答案 复制自make_uniqueandperfectforwarding(HerbSutter'sblog中同样给出)templatestd::unique_ptrmake_unique(Args&&...args){returnstd::unique_ptr(newT(s

c++ - 在 c++14 lambda 表达式中捕获和移动 unique_ptr

我以这种方式在lambda表达式中捕获unique_ptr:autostr=make_unique("mystring");autolambda=[capturedStr=std::move(str)]{cout在我尝试将capturedStr移动到另一个unique_ptr之前,它工作得很好。例如,以下内容不起作用:autostr=make_unique("mystring");autolambda=[capturedStr=std::move(str)]{cout这是编译器的输出:.../test/main.cpp:11:14:error:calltoimplicitly-dele