草庐IT

auto-ptr

全部标签

c++ - 为继承 enable_shared_from_this 的类获取 unique_ptr

通常我更喜欢从工厂返回unique_ptr。最近我遇到了为继承enable_shared_from_this的类返回unique_ptr的问题。此类的用户可能会意外调用shared_from_this(),尽管它不属于任何shared_ptr,这会导致std::bad_weak_ptr异常(或C++17之前的未定义行为,通常作为异常实现)。代码的简单版本:classFoo:publicenable_shared_from_this{stringname;Foo(conststring&_name):name(_name){}public:staticunique_ptrcreate(c

c++ - 带有自定义删除器的 std::shared_ptr 的 Typedef 别名

我想为std::shared_ptr创建别名使用自定义删除器。此代码有效,但仅适用于唯一指针。我收到有关标有[1]的行的无效模板参数数量的错误。我注意到std::unique_ptr的模板和ctor参数和std::shared_ptr与所列不同here和here我注意到这个问题可能与this重复,但我不知道如何解决我的问题#include#includetemplatestructDeleter{voidoperator()(T*p)constnoexcept{p->Drop();//SFINAE};};templateusingmy_unique_ptr=std::unique_pt

c++ - 我的 g++ 使用 vector<weak_ptr> erase() 方法生成奇怪的警告

我有以下C++代码:#include#include#include#includevoiderase_from_vector(std::vector>&mvec){for(automvec_it=mvec.begin();mvec_it!=mvec.end();)mvec_it=mvec.erase(mvec_it);}intmain(void){#if0std::vector>mvec;for(automvec_it=mvec.begin();mvec_it!=mvec.end();)mvec_it=mvec.erase(mvec_it);#endif}当我这样编译时,GCC会生成

c++ - 有条件地使初始化列表中的 shared_ptr 为 null

我处于这样一种情况,我需要使shared_ptr为null或包含类Bar的实例。下面的方法不起作用,因为Bar和nullptr不是同一类型。怎样才能做到这一点?classBar{};classFoo{private:shared_ptrb;public:Foo():b(true?Bar():nullptr){}}; 最佳答案 b(true?std::make_shared():nullptr) 关于c++-有条件地使初始化列表中的shared_ptr为null,我们在StackOverf

c++ - auto const & map 迭代器的类型是什么? C++

我需要修复我的旧项目中的一些错误,我认为这是重构部分代码的最佳时机。我有一个具有以下结构的map:std::map>ComponentMap;在我需要遍历一些底层子map的某个地方,我使用了以下内容:for(std::map::iteratoriter=ComponentMap[compNameString].begin();iter!=ComponentMap[compNameString].end();++iter){//somecodeif(IsComponentOfType(iter,sCOMP_PRINCIPAL))iter->second->GetComponentValu

c++ - `shared_ptr` 是如何实现协变的?

可以复制或构建shared_ptr来自shared_ptr(即shared_ptrptr=make_shared())。但众所周知,模板类不能相互转换,即使模板参数可以。那么如何才能shared_ptr检查它们的指针的值是否可转换,如果是则进行转换? 最佳答案 是的,默认情况下同一类模板的特化几乎没有关系,基本上被视为不相关的类型。但是您始终可以通过定义转换构造函数(To::To(constFrom&))和/或转换函数(From::operatorTo()const)来定义类类型之间的隐式转换。那又如何std::shared_ptr

c++ - 为什么 std::tr1::shared_ptr<>.reset() 如此昂贵?

分析一些大量使用shared_ptrs的代码,我发现reset()的开销出奇地大。例如:structTest{inti;Test(){this->i=0;}Test(inti){this->i=i;}};...autot=make_shared(1);...t.reset(somePointerToATestObject);跟踪最后一行中的reset()(在VC++2010下),我发现它创建了一个新的引用计数对象。有没有更便宜的方法,重用现有的引用计数并且不打扰堆? 最佳答案 在一般情况下,您不能重用现有的引用计数,因为可能有其他s

c++ 我可以将 std::unique_ptr 与依赖注入(inject)一起使用吗?

我一直在使用原始指针进行依赖注入(inject),因此我决定将我的代码转换为使用shared_ptr。这行得通,但我想知道我是否可以改用unique_ptr?在我下面的示例中,MyClass将管理信用卡服务的生命周期。classPaymentProcessor{PaymentProcessor(??creditCardService)::creditCardService_(creditCardService){}private:CreditCardService*creditCardService_;}classMyClass{public:voidDoIt(){creditCard

c++ - 使用 unique_ptr 离开范围时堆损坏

我遇到了类似于voidpointerreturnedfromfunctionheapcorruption的问题相似之处在于,当我离开使用unique_ptr的范围时,会收到“堆损坏”消息。这里是代码:voidCMyClass::SomeMethod(){std::unique_ptrspMyInterface;spMyInterface.reset(newCMyInterfaceObject());//CMyInterfaceObjectisderivedfromIMyInterfaceany_list.push_back(spMyInterface.get());//any_list

c++ - boost::python 函数调用中 boost::shared_ptr 的转换

考虑以下示例:#include"Python.h"#include#includeclassA{};classB:publicA{};voidfoo(boost::shared_ptr&aptr){}BOOST_PYTHON_MODULE(mypy){usingnamespaceboost::python;class_>("A",init());class_,bases>("B",init());def("foo",foo);}如果我调用python代码importmypyb=mypy.B()mypy.foo(b)我明白了ArgumentError:Pythonargumenttype