草庐IT

pimpl_ptr

全部标签

c++ - shared_ptr 如何破坏对齐

我正在阅读有关DirectXMath的文档,无意中发现了下一段:AsanalternativetoenforcingalignmentinyourC++classdirectlybyoverloadingnew/delete,youcanusethepImplidiom.IfyouensureyourImplclassisalignedvia__aligned_mallocinternally,youcanthenfreelyusealignedtypeswithintheinternalimplementation.Thisisagoodoptionwhenthe'public'cl

c++ - 比较 shared_ptr 对象是否相等

是否有标准谓词来比较shared_ptr托管对象的相等性。templateinlinebooltarget_equal(constT&lhs,constU&rhs){if(lhs&&rhs){return*lhs==*rhs;}else{return!lhs&&!rhs;}}我想要类似于上面代码的东西,但如果已经有标准解决方案,我会避免自己定义它。 最佳答案 不,没有标准的解决方案。shared_ptr等的相等运算符只比较指针而不比较托管对象。你的解决方案很好。我建议这个版本检查指向的对象是否相同,如果共享指针之一为null而另一个

c++ - std::shared_ptr operator [] 等效访问

在C++17中,std::shared_ptr有一个运算符[]以允许索引基于vector的指针(http://en.cppreference.com/w/cpp/memory/shared_ptr/operator_at)如果这样的运算符不可用,我如何获得类似的访问,我仍然想对元素数组使用智能指针,例如:std::shared_ptrdata;data.reset(newunsignedchar[10]>;//usedata[3]; 最佳答案 像这样:data.get()[3]但是,请记住Nathan在评论中所说的话。std::sh

c++ - `weak_ptr` 和 `shared_ptr` 访问如何是原子的

std::shared_ptrint_ptr;intmain(){int_ptr=std::make_shared(1);std::threadth{[&](){std::weak_ptrint_ptr_weak=int_ptr;autoint_ptr_local=int_ptr_weak.lock();if(int_ptr_local){cout上面的代码线程安全吗?我读了这个答案Aboutthread-safetyofweak_ptr但只是想确保上面的代码是线程安全的。我问这个的原因是,如果上面的代码确实是线程安全的,我无法理解std::weak_ptr是如何实现的。和std::s

c++ - 通过采用 void * 的 C 接口(interface)传递 shared_ptr

我有一个使用SDL的C++项目,特别是SDL事件。我想将事件系统用于传入的网络消息,就像它用于UI事件一样。我可以定义一个新的事件类型并附加一些任意数据(参见thisexample)。如果我使用普通指针,这就是我会做的:Uint32message_event_type=SDL_RegisterEvents(1);/*Inthemaineventloop*/while(SDL_Poll(&evt)){if(evt.type==message_event_type){Message*msg=evt.user.data1;handle_message(msg);}}/*Networkingc

c++ - "Use of undefined type"带有 unique_ptr 以转发声明的类和默认移动构造函数/赋值

在下面的代码中,是避免编译错误并在A.cpp中手动包含B.h实现移动构造函数/赋值的唯一方法吗?//A.h#includeclassB;//implementationsomewhereinB.h/B.cppclassA{public:A()=default;~A()=default;A(constA&)=delete;A&operator=(constA&)=delete;A(A&&)=default;A&operator=(A&&)=default;private:std::unique_ptrm_b;};VisualStudio2015给出“错误C2027:使用未定义类型”,因为

c++ - std::unique_ptr 删除函数,initializer_list - 驱动分配

全部,当我使用初始化列表格式实例化小部件数组时,指向成员变量小部件实例的裸指针可以编译,但在更改为std::unique_ptr后,gcc会给出有关已删除函数的编译错误。$uname-aLinux..3.5.0-21-generic#32-UbuntuSMP2012年12月11日星期二18:51:59UTCx86_64x86_64x86_64GNU/Linux$g++--versiong++(Ubuntu/Linaro4.7.2-5ubuntu1)4.7.2此代码给出以下编译器错误:#include#includeclassWidget{public:Widget(){}};class

c++ - 为什么 observer_ptr 在 move 后不归零?

为什么不是observer_ptrmove操作后归零?它在默认构造中被正确设置为nullptr,这确实有意义(并防止指向垃圾)。相应地,当std::move()开始时,它应该被清零,就像std::string、std::vector等这将使它成为多个上下文中的一个很好的候选者,在这些上下文中,原始指针是有意义的,并且自动在具有原始指针数据成员的类上生成move操作,例如thiscase.编辑正如@JonathanWakely在评论中指出的(与aforementionedquestion相关):ifobserver_ptrwasnullafteramoveitcanbeusedtoimp

c++ - 通过取消引用复制 std::unique_ptr 的值

我写了下面的代码,我试图将unique_ptr对象的值复制到一个结构中。#include#includeusingnamespacestd;structS{S(intX=0,intY=0):x(X),y(Y){}//S(constS&){}//S&operator=(constS&){return*this;}intx;inty;std::unique_ptrptr;};intmain(){Ss;s.ptr=std::unique_ptr(newS(1,4));Sp=*s.ptr;//Copythepointer'svaluereturn0;}它在VisualC++2012中弹出错误:

c++ - "Connecting"SDL_Surface 到 shared_ptr

我想知道如何将SDL_Surface*与shared_ptr连接起来?在删除SDL_Surface之前,我需要调用SDL_FreeSurface(SDL_Surface*)。如何在shared_ptr中“修改删除过程”? 最佳答案 只需将SDL_FreeSurface传递给构造函数:std::shared_ptrshared_surf(SDL_LoadBMP("foo.bmp"),SDL_FreeSurface);请确保您不使用SDL_SetVideoMode或SDL_GetVideoSurface返回的指针执行此操作。