weak_intrusive_pointer
全部标签 我想将对对象的引用存储为weak_ptr。在纯C++中,以下工作:#include#include#includeusingnamespacestd;usingnamespaceboost;structEmpty{Empty(){}};structStore{weak_ptrvalue;Store(){};voidsetValue(shared_ptrv){coutvalue=weak_ptr(v);shared_ptrv_ok=this->value.lock();if(v_ok){coutgetValue(){shared_ptrp=this->value.lock();if(p)
我有一个用于跟踪对象的weak_ptr列表。在某个时候,我想从给定shared_ptr或weak_ptr的列表中删除一个项目。#includeintmain(){typedefstd::list>intList;std::shared_ptrsp(newint(5));std::weak_ptrwp(sp);intListmyList;myList.push_back(sp);//myList.remove(sp);//myList.remove(wp);}但是,当我取消对以上行的注释时,程序将无法构建:1>c:\programfiles(x86)\microsoftvisualstu
使用指向基类的指针调用类的虚成员函数当然是C++中非常常见的事情。所以我觉得很奇怪,当你有一个成员指针而不是一个普通指针时,似乎不可能做同样的事情。请考虑以下代码:structB{virtualvoidf();};structD:B{virtualvoidf();};structE{Bb;Dd;};intmain(){Ee;//Firstwithnormalpointers:B*pb1=&e.b;//OKB*pb2=&e.d;//OK,BisabaseofDpb1->f();//OK,callsB::f()pb2->f();//OK,callsD::f()//Nowwithmember
在C++11或C++1y/14中:给定一个TC::*形式的指向成员类型的值,我想得到指向类型例如:#includeusingnamespacestd;structT1{staticvoidf(){coutstructV;templatestructV{staticconstexprT1U1::*pm=&U1::x;};templatestructV{staticconstexprT2U2::*pm=&U2::x;};templatevoidf(Wpm){typedef???T;T::f();}intmain(){f(V::pm);f(V::pm);}有没有办法做到这一点?上面的???是
问题以下代码无法在C++11(或C++14)中编译。我理解编译器的错误输出,但为什么标准不允许?//main.cpp#includeintmain(void){doublea=3.0;doubleb=3.0;//Itworkswithmerepointersconstdouble*ptrToConst=&a;/***/double*ptrToObj=&a;//ptrToObj=ptrToConst;//Illegal:that'sunderstandable…ptrToConst=ptrToObj;//Works//Butthesamedoesn'tworkwithvectorstop
这个问题在这里已经有了答案:Witharrays,whyisitthecasethata[5]==5[a]?(19个回答)Whydoesx[y]==y[x]inc++?[duplicate](3个答案)关闭8年前。据我所知,可以编写以下代码:char*a=newchar[50];for(inti=0;i它编译。有用。它完全与相同char*a=newchar[50];for(inti=0;i仅仅是因为:a[b]默认情况下作为宏*(a+b)实现,两个代码示例都有效的事实只是一个意外/特定于编译器它已在某处标准化,此类算法的结果在每个平台上都应该相同假设加法应该是可交换的是合理的,但如果我们
我在使用std::is_member_function_pointer时遇到问题。据我所知,在给定noexcept成员函数时它不起作用。我在标准中找不到任何声明它不适用于noexcept合格成员函数的内容。问题示例:#includeclassA{public:voidmember()noexcept{}};intmain(){//failsatcompiletimeifA::memberisadatamemberandnotafunctionstatic_assert(std::is_member_function_pointer::value,"A::memberisnotamemb
我收藏了Creature使用std::make_shared在我的应用程序的一部分中创建和拥有的对象和std::shared_ptr.我还跟踪了零个或一个的选择Creature在World使用std::weak_ptr的对象.voidWorld::SetSelection(conststd::shared_ptr&creature){selection=creature;}std::shared_ptrWorld::GetSelection()const{returnselection.lock();}GetSelection的来电者负责检查指针是否为空。如果是,则表示当前没有选择。T
请顾及我的经验不足,不明白std::owner_less的意义.我已经shown那一个map与weak_ptr不推荐作为key,因为已过期weak_ptrkey会破坏map,实际上:Ifitexpires,thenthecontainer'sorderisbroken,andtryingtousethecontainerafterwardswillgiveundefinedbehaviour.这种行为有多不确定?我问的原因是因为docs说说owner_less:Thisfunctionobjectprovidesowner-based(asopposedtovalue-based)mi
我学到了很多关于weak_ptr与share_ptr一起使用来打破循环引用的知识。它是如何工作的?如何使用它?任何人都可以给我一个例子吗?我完全迷失在这里。还有一个问题,什么是强指针? 最佳答案 强指针持有对象的强引用——意思是:只要指针存在,对象就不会被销毁。对象不“知道”每个指针,只知道它们的编号——这就是强引用计数。weak_ptr会“记住”对象,但不会阻止它被销毁。你不能通过弱指针直接访问对象,但你可以尝试从弱指针创建一个强指针。如果该对象不再存在,则生成的强指针为空:shared_ptrsp(newint);weak_pt