草庐IT

my_shared_ptr

全部标签

c++ - 你可以使用 boost::shared_ptr 作为 map 的键吗?

我可能需要重新考虑我的整体设计,但就目前而言,看起来我可能想做以下事情:classA;classB;std::map,B>APtrToBMap;我试过这个,它似乎在一个简单的情况下工作(编译器没有提示,简单的测试似乎工作)。但我对这种方法有了第二个想法。我怀疑其中有一些我不知道的问题。那么,以上内容在实际意义上有效吗?或者我这样做时是否有一些我不知道的缺陷? 最佳答案 引用有这样的说法:templatebooloperatorconst&a,shared_ptrconst&b);//neverthrows返回:一个未指定的值,使得o

c++ bad_weak_ptr 错误

我想创建一些Timer类,它每N秒打印一次“文本”,其中N将在构造函数中初始化。#include#include#include#includeclassTimer:publicboost::enable_shared_from_this{public:Timer(constdoubleinterval):interval_sec(interval){io_service_=newboost::asio::io_service;timer_=newboost::asio::deadline_timer(*io_service_);start();io_service_->run();}

c++ - 自动克隆 unique_ptr

std::unique_ptr有一个已删除的复制构造函数,这意味着如果你的类Foo中有一个unique_ptr作为数据成员那么您必须为Foo编写自己的复制构造函数并手动深度复制该成员(即使编译器生成的复制构造函数对所有其他成员都可以)。为了能够以多态方式进行复制,可以使用clone()方法模式。假设我们的对象有一个这样的克隆方法:classBase{virtualstd::unique_ptrclone()=0;};Foo现在看起来像这样:classFoo{public:...Foo(Fooconst&other):b(other.b->clone()),//init10moremem

C++11:GCC 4.8 静态 thread_local std::unique_ptr 未定义引用

我需要为将通过宏访问的每个线程存储一个唯一指针。我想我应该用一个单例和静态thread_localstd::unique_ptr对象来解决这个问题。这是代码的简化版本:main.cpp#include#include#include#includeusingnamespacestd;#include"yay.hpp"mutexcoutMutex;voidyay(intid){int*yayPtr=getYay();//IknowthisisbadcoutMutex.lock();couthappy;for(inti=0;i耶.hpp#ifndefBE_HAPPY#defineBE_HA

c++ - 为什么 auto_ptr 似乎违反了 Visual C++ 上的私有(private)继承?

背景信息:这是在VisualStudio2008上检测到的,并在VisualStudio2013上再次确认。G++对代码大喊大叫,而Visual默默地接受了私有(private)继承漏洞。所以,在VisualC++上,我们有以下代码:classBase{};classDerived:Base{};//inheritsprivately.Addingexplicitlythe//keywordprivatechangesnothingintmain(){std::auto_ptr(newDerived);//compiles,whichisNOTEXPECTEDstd::auto_ptr

c++ - 我应该停止使用 auto_ptr 吗?

我最近开始欣赏std::auto_ptr,现在我读到它将是deprecated.我开始在两种情况下使用它:工厂的返回值传达所有权转让例子://Exceptionsafeandmakesitclearthatthecallerhasownership.std::auto_ptrComponentFactory::Create(){...}//Thereceivingmethod/functiontakesownershipofthepointer.Zeroambiguity.voidsetValue(std::auto_ptrinValue);尽管存在有问题的复制语义,但我发现auto_

c++ - 我可以将 std::make_shared 与没有参数构造函数的结构一起使用吗?

假设我有一个像这样的struct:structS{inti;doubled;std::strings;};我可以这样做吗?std::make_shared(1,2.1,"Hello") 最佳答案 不,您不能,您必须定义自己的构造函数才能做到这一点。#include#include#includestructS{S(intii,doubledd):i(ii),d(dd){}inti;doubled;};intmain(){//Ss{1,2.1};autos=std::make_shared(1,2.1);//orwithoutcons

c++ - std::enable_shared_from_this;公共(public)与私有(private)

我使用shared_ptr和enable_shared_from_this玩了一会儿,但遇到了一些我不太了解的东西。在我的第一次尝试中,我构建了这样的东西:classshared_test:std::enable_shared_from_this{public:voidprint(boolrecursive){if(recursive){shared_from_this()->print(false);}std::cout请注意,这个类正在私下扩展std::enable_shared_from_this。这显然有很大的不同,因为执行这样的事情:intmain(){autot(std::

c++ - std::unique_ptr<T[]> API 禁止派生到基指针的转换

在ModernEffectiveC++中,“Iterm19:使用std::shared_ptr进行共享所有权资源管理。”,第133-134页,它说:std::shared_ptrsupportsderived-to-basepointerconversionsthatmakesenseforsingleobjects,butthatopenholesinthetypesystemwhenappliedtoarrays.(Forthisreason,thestd::unique_ptrAPIprohibitssuchconversions.)“类型系统中的漏洞”是什么意思?为什么std:

c++ - 为什么不能推导出unique_ptr的模板参数?

当你有C++17中可用的类模板参数推导时,为什么你不能推导std::unique_ptr的模板参数?例如,这给了我一个错误:std::unique_ptrsmp(newD);上面写着“类模板的参数列表丢失”。模板参数(至少是指针类型)不应该是可推导的吗?Seethis:anydeclarationthatspecifiesinitializationofavariableandvariabletemplate 最佳答案 让我们看看newint和newint[10].两者都返回int*.无法判断您是否应该拥有unique_ptr或un