草庐IT

pimpl_ptr

全部标签

c++ - 在 C++ 静态 boost::shared_ptr 中保存 python 生成的值

在使用Boost.Python和C++时,有时我们会创建使用类本身和boost::shared_ptr绑定(bind)的类。版本。出于多种原因,这非常方便,可以在很多地方使用。但是,当boost::python时,该机制似乎无法可靠地工作。返回boost::shared_ptr为在Python中生成并记录在C++静态变量中的值。通常,我希望返回boost::shared_ptr持有一个特殊的删除器来处理这个问题,但事实并非如此。似乎发生的是返回的boost::shared_ptr只是包装一个指向在Python中产生的值的指针,没有任何关于删除的特殊考虑。这会导致来自双重删除的一致崩溃(

c++ - 前向声明、unique_ptr 和类内初始化器

我已阅读Isstd::unique_ptrrequiredtoknowthefulldefinitionofT?和Forwarddeclarationwithunique_ptr?,但我的问题更具体。以下编译://Compilewith$g++-std=c++11-c#includeclassA;//fwddeclarationclassAUser{AUser();//definedelsewhere~AUser();//definedelsewherestd::unique_ptrm_a;};以下不是://Compilewith$g++-std=c++11-c#includeclas

c++ - 替代 PImpl Idiom - 优势与劣势?

传统的PImplIdiom是这样的:#includestructBlah{//publicinterfacedeclarationsprivate:structImpl;std::unique_ptrimpl;};//insourceimplementationfile:structBlah::Impl{//privatedata};//publicinterfacedefinitions然而,forfun,Itried改为使用具有私有(private)继承的组合:[测试.h]#include#includetemplatestructPImplMagic{PImplMagic(){s

c++ - 使用 unique_ptr 作为方法参数 - 优点和缺点

我注意到如果我有以下内容:#includeusingnamespacestd;classFoo{public:Foo();};classWobble{public:voidSetWibble(unique_ptrfoo){this->wibble=move(foo);}//Ilikereturningarefasitgivescontrolto//theuserofmyframeworkoverrecievinga&oracopyFoo&GetWibble(){return*wibble;}unique_ptrwibble;};int_tmain(intargc,_TCHAR*argv

c++ - unique_ptr、nullptr 并支持 gcc 4.5.x 和 4.6.x

我正在开发一个有两个不同最终用户的库,其中一个使用gcc4.5.3,另一个刚刚迁移到gcc4.6.3。该库使用新的C++11智能指针(特别是unique_ptr)并在gcc4.5.3上编译良好。然而,在这两个版本之间,gcc开始支持nullptr,因此unique_ptr的API发生了变化,以更接近标准。现在这样做,下面的代码从好到模棱两可unique_ptrup(newint(30));...if(up==0)//ambiguouscallnowtounique_ptr(int)for0是否有一种干净的(即,下一句)方法来更改上面的if语句,以便它在有和没有nullptr的情况下都有

C++11 : unique_ptr complains about incomplete type, 但是当我包装它时不是

SO上已经有很多关于unique_ptr和不完整类型的问题,但没有一个能给我一个概念来理解为什么以下内容不起作用://error:...std::pair::secondhasincompletetypetemplatestructImpl{typedeftypenamestd::unordered_map>::iteratoriter_type;std::unique_ptrptr;Impl():ptr(newiter_type()){}};intmain(){Impl();return0;}而以下是:templatestructImpl{structWrapper{typedeft

c++ - 将 `std::default_delete` 专门化为 `std::shared_ptr`

我有这样的想法:namespacestd{templateclassdefault_delete{public:voidoperator()(IplImage*ptr)const{cvReleaseImage(&ptr);}};};typedefstd::shared_ptrIplImageObj;我没有真正找到太多信息是否支持我专门化default_delete以及shared_ptr是否也默认使用default_delete。它的工作方式与Clang5.0.0的预期一致。那么,支持吗?如果STL实现有不同的内部命名空间怎么办?那它不会找到我的声明吗?但它应该会在声明中出错。

c++ - 如何将 std::unique_ptr<T> 与返回 int 的接口(interface)一起使用?

我想将open/closePOSIXAPI包装到一个RAII兼容对象中,例如std::unique_ptr。但是open函数返回一个int(即不是HANDLE,它是指向void的指针),并且我不确定如何将std::unique_ptr模板类与int一起使用。有人可以帮帮我吗? 最佳答案 真的,您想要的只是让close(intfileHandle)为您管理,对吗?为什么不创建一个带有为您调用close()的析构函数的简单C++类?我认为这就是您要寻找的行为。std::shared_ptr,friend只处理用new创建的堆指针,会调用

c++ - 如何深入研究 shared_ptr [Netbeans、clang++、gdb]

我正在使用NetbeansC++8.0.2clang++(Ubuntuclang版本3.6.0-2ubuntu1(tags/RELEASE_360/final)(基于LLVM3.6.0))gdb(GNUgdb(Ubuntu7.9-1ubuntu1)7.9)在我的“C++简单测试”中,每当我检查一个shared_ptr变量时,我看到的所有值都是:std::shared_ptr(count1,weak0)0x64d3a0或类似的。无法深入了解它实际指向的值。即使变量窗口中的TreeView显示了其中一个扩展器图标,当我单击它时它也会消失。当我尝试取消引用它或在“表达式”窗口中调用它的get

c++ - 防止对 std::unique_ptr 的不安全取消引用

摘自cppcon2015的幻灯片:unique_ptrf(){autoa=make_unique();returna;}//Whydoesthisevencompile?constA&dangling=*f();//BOOM!!!use(dangling);我的问题是:对于*this的右值引用,这可以解决吗?我在cppreference的规范中看到:typenamestd::add_lvalue_reference::typeoperator*()const;问题:不允许operator*用于右值unique_ptr并且只对左值unique_ptr取消引用有效吗?仍然有有效的用例来保持