草庐IT

weak_alias

全部标签

c++ - 构造一个 constexpr std::weak_ptr

根据std::weak_ptr文档可以构造一个constexprweak_ptr:#includeconstexprweak_ptrfoo{};但是,使用clang尝试此操作会产生编译错误,提示constexpr变量不能具有非文字类型'conststd::weak_ptr',这是因为weak_ptr有一个用户提供的析构函数。(确实如此,查看libc++header)我的问题是,这是一个libc++错误,还是做constexprweak_ptr只是没有意义并且拥有constexpr默认构造函数是一个错误?我可以期待它在未来发挥作用吗? 最佳答案

c++ - weak_ptr 和父子循环依赖

我目前有类似以下内容:classParent{//justasinglechild...forsakeofsimplicity//nootherclassholdsashared_ptrreferencetochildshared_ptr_child;System*getSystem(){...}}classChild{weak_ptr_parent;~Child{_parent.lock()->getSystem()->blah();}}Child析构函数总是崩溃,因为当~Child()运行时_parent总是过期。是否有针对这种怪现象的典型解决方案?简而言之,有没有办法在~Chil

C++ STL:将派生虚拟类用作 std::sort() 的 "Strict Weak Ordering"

我使用std::sort()撞墙了。我有一个纯虚类(名为Compare),方法的调用者派生自该类(名为MyComp)。我将纯虚拟类用于我的API原型(prototype):voidObject::DoSort(Compare&comp){std::sort(this->mKeys.begin(),this->mKeys.end(),comp);}来电者:classMyComp:publicCompare{booloperator()(constRow*r1,constRow*r2){...}}cmp;...obj->DoSort(cmp);Linux上的g++编译器提示:“无法分配类型

c++ - weak_ptr 奇怪的复制构造函数

以下是weak_ptr的2个构造函数:http://msdn.microsoft.com/en-us/library/bb982126.aspxweak_ptr(constweak_ptr&);templateweak_ptr(constweak_ptr&);实际代码(来自内存):weak_ptr(constweak_ptr&_Other){//constructweak_ptrobjectforresourcepointedtoby_Otherthis->_Resetw(_Other);}templateweak_ptr(constweak_ptr&_Other,typenameen

c++ - weak_ptr C++ 中的比较运算符

我在新的STL成员中还是个新手。谁能指出为什么这段代码会出现段错误?#include#include#include#include#includeusingnamespacestd;structStubClass{weak_ptrb;intc;friendbooloperator==(StubClassx,StubClassy);friendbooloperatorx):b(x){c=5;}};booloperator==(StubClassd,StubClassc){returnd.b==c.b;}booloperatorspPtr(newstring("Hello"));weak

c++ - map 中过期的 weak_ptr 会发生什么

我想了解weak_ptr已过期的映射中的条目(类型为boost::weak_ptr)会发生什么。map中的相应条目是否会自动删除?键是一个整数,对应的值是一个weak_ptr。我写的示例代码,但无法编译#include#include#includeusingnamespacestd;classFoo:publicboost::enable_shared_from_this{public:Foo(intn=0):bar(n){std::coutinc_ref(){returnshared_from_this();}private:intbar;};std::map>mappy;intm

c++ weak_ptr在取消引用后过期?

我是智能指针的新手,我正在思考为什么weak_ptr在取消引用运算符后会过期。我用来测试的代码在这里:#include#include#includeusingnamespacestd;structnode{weak_ptrparent;shared_ptrchild;intval;};shared_ptrfoo(){shared_ptra=make_shared();shared_ptrb=make_shared();a->val=30;b->val=20;b->parent=a;a->child=b;returna;}intmain(){shared_ptrc=foo();node

c++ - 尝试理解 std::enable_shared_from_this<T> 但使用它会导致 bad_weak_ptr

我试图理解std::enable_shared_from_this类的行为,但我无法理解。所以我写了一个简单的程序来测试不同的情况。问题谁能解释一下下面代码的行为,因为我无法解释观察到的结果。谢谢你的帮助。代码#include#includestructC:std::enable_shared_from_this{};intmain(){{//test1std::shared_ptrfoo,bar;foo=std::make_shared();bar=foo->shared_from_this();//okstd::coutfoo=std::shared_ptr(newC);std::

c++ - 在库接口(interface)中提供对 weak_ptr 的访问是否明智?

我编写了一个库,公开了对多个相关对象类型的引用。所有这些对象的生命周期都由库在内部通过boost::shared_ptr管理。根据库的性质,库的用户还可以知道任何公开对象的生命周期。所以他们可以存储指针或保留对这些对象的引用。他们这样做并知道这些对象何时不再有效是合理的。但我对强制我的用户讲道理感到内疚。公开库是否可以接受weak_ptr的对象?其他图书馆有这样做吗?我已经分析了这个库在应用程序中的使用情况,发现它对任务至关重要,无法公开weak_ptr独家。让匹配的API函数公开一个引用或一个weak_ptr或使任何对象能够公开一个weak_ptr是否更明智?对自己?

c++ - 为什么我们需要 C++11 中的 weak_ptr?

我正在阅读NicolaiM.Josuttis的“TheC++StandardLibrary”一书以了解弱指针。作者提到了需要weak_ptr的两个原因,我不明白第二个原因。任何人都可以提供一个简单的解释以及以下原因的示例(引自书中):Anotherexampleoccurswhenyouexplicitlywanttosharebutnotownanobject.Thus,youhavethesemanticsthatthelifetimeofareferencetoanobjectoutlivestheobjectitrefersto.Here,shared_ptrswouldnev