草庐IT

enable-shared

全部标签

c++ - 我可以使用placement new 来重置shared_ptr 中的对象吗?

假设我有一个类。classBigData{...};typedefboost::shared_ptrBigDataPtr;然后我做:BigDataPtrbigDataPtr(newBigData());稍后在我完成我的对象之后,我确信该对象没有其他用户。执行以下操作是否安全:bigDataPtr->~BigDataPtr();new(&*bigDataPtr)BigData;这会让我在没有任何额外分配的情况下重置对象吗? 最佳答案 有几种方法可以解决这个问题。您可以使用placementnew,这可以保证是安全的,原因有两个:您已经

c++ - 我可以使用placement new 来重置shared_ptr 中的对象吗?

假设我有一个类。classBigData{...};typedefboost::shared_ptrBigDataPtr;然后我做:BigDataPtrbigDataPtr(newBigData());稍后在我完成我的对象之后,我确信该对象没有其他用户。执行以下操作是否安全:bigDataPtr->~BigDataPtr();new(&*bigDataPtr)BigData;这会让我在没有任何额外分配的情况下重置对象吗? 最佳答案 有几种方法可以解决这个问题。您可以使用placementnew,这可以保证是安全的,原因有两个:您已经

c++ - C++11 中 shared_ptr 的原子操作

通过阅读c++11草案n3242,第20.7.2.5节,看起来我们对shared_ptr进行了原子操作,这使我们能够对复杂结构进行无锁操作,而无需担心GC/内存泄漏。但是,我无法在GCC-4.7.0中成功使用它。我只是测试了以下程序#include#include#includestructX{intx;doubley;std::strings;};intmain(){std::shared_ptrx(newX);autop=std::atomic_load(&x);}它有编译器错误:c.cpp:13:33:error:nomatchingfunctionforcallto‘atomi

c++ - C++11 中 shared_ptr 的原子操作

通过阅读c++11草案n3242,第20.7.2.5节,看起来我们对shared_ptr进行了原子操作,这使我们能够对复杂结构进行无锁操作,而无需担心GC/内存泄漏。但是,我无法在GCC-4.7.0中成功使用它。我只是测试了以下程序#include#include#includestructX{intx;doubley;std::strings;};intmain(){std::shared_ptrx(newX);autop=std::atomic_load(&x);}它有编译器错误:c.cpp:13:33:error:nomatchingfunctionforcallto‘atomi

c++ - boost::shared_ptr<T> 和 boost::shared_ptr<const T> 是否共享引用计数?

关于boost::shared_ptr的陷阱有几个有趣的问题。s。其中之一是避免指向boost::shared_ptr的有用提示。和boost::shared_ptr到Derived类型的同一对象因为它们使用不同的引用计数并可能过早地销毁对象。我的问题:同时拥有boost::shared_ptr是否安全?和boost::shared_ptr指向T类型的同一对象,或者这会导致同样的问题吗? 最佳答案 绝对安全。以下代码示例:#include#includeintmain(int,char**){boost::shared_ptra(n

c++ - boost::shared_ptr<T> 和 boost::shared_ptr<const T> 是否共享引用计数?

关于boost::shared_ptr的陷阱有几个有趣的问题。s。其中之一是避免指向boost::shared_ptr的有用提示。和boost::shared_ptr到Derived类型的同一对象因为它们使用不同的引用计数并可能过早地销毁对象。我的问题:同时拥有boost::shared_ptr是否安全?和boost::shared_ptr指向T类型的同一对象,或者这会导致同样的问题吗? 最佳答案 绝对安全。以下代码示例:#include#includeintmain(int,char**){boost::shared_ptra(n

c++ - std::make_shared、std::unique_ptr 和移动构造函数

以下代码使用clang3.0/libc++编译:#includeclassFoo{public:Foo():mem_(newint(10)){}std::unique_ptrmem_;};intmain(){autofoo=std::make_shared();return0;}但是这个没有(std::string参数加了):#include#includeclassFoo{public:Foo(conststd::string&s):mem_(newint(10)){}std::unique_ptrmem_;};intmain(){autofoo=std::make_shared("

c++ - std::make_shared、std::unique_ptr 和移动构造函数

以下代码使用clang3.0/libc++编译:#includeclassFoo{public:Foo():mem_(newint(10)){}std::unique_ptrmem_;};intmain(){autofoo=std::make_shared();return0;}但是这个没有(std::string参数加了):#include#includeclassFoo{public:Foo(conststd::string&s):mem_(newint(10)){}std::unique_ptrmem_;};intmain(){autofoo=std::make_shared("

c++ - 使用 enable_if 检查参数包的类型

由于对allowednon-typevariadictemplates有限制,我正在尝试使用enable_if编写一个采用任意数量double的函数。本质上,我想做这样的事情:template::value,T>::type>foo(T...t){/*codehere*/}我选择将enable_if作为未命名参数的默认值,因为我的函数实际上是一个构造函数并且没有返回值。这适用于单个参数,但由于它是可变参数模板,T是一个参数包,上面的代码无效。那么,如何检查每个参数都可以转换为double值呢? 最佳答案 bool_pack把戏又来了

c++ - 使用 enable_if 检查参数包的类型

由于对allowednon-typevariadictemplates有限制,我正在尝试使用enable_if编写一个采用任意数量double的函数。本质上,我想做这样的事情:template::value,T>::type>foo(T...t){/*codehere*/}我选择将enable_if作为未命名参数的默认值,因为我的函数实际上是一个构造函数并且没有返回值。这适用于单个参数,但由于它是可变参数模板,T是一个参数包,上面的代码无效。那么,如何检查每个参数都可以转换为double值呢? 最佳答案 bool_pack把戏又来了