草庐IT

gray8_ptr_t

全部标签

c++ - 如果异常保存在 std::exception_ptr 中,那么在 catch 语句之外使用异常是否安全?

我有一个std::exception_ptr里面有一个异常。我将调用std::rethrow_exception来获取实际的异常,异常在catch语句之后是否有效?我的猜测是因为我仍然持有std::exception_ptr它仍然有效。看例子:std::exception_ptrePtr=initialize_somewhere_else();std::runtime_error*a=NULL;try{std::rethrow_exception(ePtr);}catch(std::runtime_error&e){a=&e;}std::coutwhat()注意:在我使用Clang的测

C++如何将unique_ptr的队列添加到 vector

简化代码:#include#include#includeclassFoo{public:Foo(){};virtual~Foo(){}};intmain(){std::queue>queue;autoelement=std::make_unique();queue.push(std::move(element));std::vector>>vector;//Error1vector.push_back(queue);//Error2vector.push_back(std::move(queue));//Error3vector.push_back({});return0;}错误:'

c++ - QList 和 shared_ptr

你怎么看?这是正确的还是存在内存泄漏?来源:#include#include#includeclassA{private:intm_data;public:A(intvalue=0){m_data=value;}~A(){std::cout>list;list.append(boost::shared_ptr(newA(6)));std::cout输出:6destroyingA(6) 最佳答案 看来是对的。Boost的shared_ptr是一个引用计数指针。如果对象之间没有循环引用,引用计数能够回收内存。在您的情况下,类A的对象不引

c++ - std::unique_ptr 结构成员到结构类型

是structA{std::unique_ptra;};标准允许吗?我不认为它适用于像std::set这样的容器类型,但是关于unique_ptr有什么特别吗? 最佳答案 是的,这是明确允许的。C++14(n4140)20.8.1/5:...ThetemplateparameterTofunique_ptrmaybeanincompletetype.std::shared_ptr和std::weak_ptr也允许使用类似的措辞。 关于c++-std::unique_ptr结构成员到结构类

c++ - unique_ptr 的 deque vector 的编译器错误

以下代码无法在gcc5.3上编译,编译器错误提示unique_ptr的复制构造函数以某种方式被调用。有人可以解释为什么会这样吗?#include#include#includeusingFoo=std::deque>;voidfoo(){std::vectora;a.emplace_back();//thisfailstocompile}编译错误中的关键行是:gcc-4.9.2/include/c++/4.9.2/bits/stl_construct.h:75:7:error:useofdeletedfunction‘std::unique_ptr::unique_ptr(consts

c++ - 了解 shared_ptr 别名构造函数的原型(prototype)

shared_ptr别名构造函数的原型(prototype),形式为g++:templateshared_ptr(constshared_ptr&__r,element_type*__p)noexcept:__shared_ptr(__r,__p){}这里给出的例子是:shared_ptr>pii(newpair());shared_ptrpi(pii,&pii->first);行得通;一直有效。但是看看原型(prototype),_Yp是我们提供的用于实例化模板的模板参数,因此上面的最后一行感觉应该是:shared_ptrpi(pii,&pii->first);但是这个例子绝对是是正

c++ - 将 unique_ptr<Object> 作为 unique_ptr<const Object> 返回

我有这样的方法:std::unique_ptrTable::GetStats()const{std::unique_ptrresult;//...//Preparestats.Undersomeconditionsanexceptionmaybethrown.//...returnresult;}问题是它无法编译:error:cannotbind‘std::unique_ptr’lvalueto‘std::unique_ptr&&’我可以使用以下绕过方法使其编译:returnstd::unique_ptr(result.release());不过好像有点过分了。我无法理解,从C++的角

c++ - 将 shared_ptr 转换为 void*

我正在使用libev,它需要将我的数据转换为void*以符合其预定义的结构。我需要将boost::shared_ptr转换为void*,然后将void*转换回boost::shared_ptr。这是我执行此操作的代码voidfoo(boost::shared_ptr&a_string){void*data=(void*)a_string.get();boost::shared_ptrmyString((string*)data);}我很确定这工作正常,但是我的代码设置方式我相信所有对我的字符串的shared_ptr引用都超出了范围,因为这种转换方法不会增加use_count,因此sha

c++ - 大 vector 的返回值优化与 auto_ptr

如果我使用auto_ptr作为填充大vector的函数的返回值,这会使该函数成为源函数(它将创建一个内部auto_ptr并在返回非constauto_ptr时移交所有权)。但是,我不能将此函数与STL算法一起使用,因为为了访问数据,我需要取消对auto_ptr的引用。我猜一个很好的例子是大小为N的vector场,每个vector有100个分量。如果N很大,则函数按值或按ref返回每个100个分量vector是不一样的。此外,当我尝试这个非常基本的代码时:classt{public:t(){std::coutautoFun(){returnstd::auto_ptr(newt());}a

c++ - const 正确性和 shared_ptr,设计问题?

我最近开始尝试在我的代码中强制执行const正确性。在函数定义中,我将常量指针提供给类LorentzM的常量对象。:voidAnalysisObject::SetOwnedPointer(constintmaptotree_In,constLorentzM*constmomentum_In){owned_pp4_original.reset(momentum_In);maptotree=maptotree_In;}哪里owned_pp4_original是shared_ptrowned_pp4_original;我这样做是因为这个函数SetOwnedPointer永远不应该改变Lore