草庐IT

Shared_ptr

全部标签

C++11:将 std::is_pointer 扩展为 std::shared_ptr

我想重载std::is_pointer在C++11中为std::shared_ptr生成真值同样,因为后者的行为非常类似于T*.#includenamespacestd{templatestructis_pointer>:std::true_type{};templatestructis_pointer>:std::true_type{};}我想知道为什么这个重载还没有包含在标准实现中。有没有我忽略的陷阱?当然也可以引入一个新特性is_shared_ptr.其实我一开始就尝试了下面的代码:templatestructis_pointer::type>>:std::true_type{}

c++ - 使用 shared_ptr 的开销和实现

简短介绍:我正在处理多线程代码,我必须在两个线程之间共享动态分配的对象。为了使我的代码更简洁(并且不易出错),我想在每个线程中显式“删除”对象,这就是我想使用shared_ptr的原因。第一个问题:我想知道shared_ptr中->operator的实现在运行时是否有一些额外的开销(例如,大于unique_ptr)。我所说的对象通常是longlife实例,创建后只复制一次(当我在线程之间分发它们时),然后我只访问这些对象的方法和字段。我知道,shared_ptr只保护引用计数。第二个问题:shared_ptr在libstdc++中的优化程度如何?它总是使用互斥锁还是利用原子操作(我专注

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::