草庐IT

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

c++ - std::unique_ptr constexpr 构造函数

如图here,std::unique_ptr有两个用于空指针的constexpr构造函数:constexprunique_ptr();constexprunique_ptr(nullptr_t);我对这两个构造函数有两个问题:为什么我们需要两个?我们不能只声明一个:constexprunique_ptr(nullptr_t=nullptr);constexpr真的有用吗?我尝试在我的代码中这样做,但它没有编译(g++6.1.0,-std=c++14):constexprstd::unique_ptrp;//error:thetype'conststd::unique_ptr'ofcon

c++ - shared_ptr 的自定义删除器的附加参数

无论如何我可以向std::shared_ptr的删除器发送参数吗?感觉像:std::shared_ptrmyA(a,myDeleter(a,5));myDeleter有这个签名:voidmyDeleter(A*a,inti)(显然上面的语法是错误的,但只是为了强调我需要我的删除器来接受额外的参数。) 最佳答案 您可以在将删除器的第二个参数作为删除器传递之前std::bind:autodeleter=std::bind(myDeleter,std::placeholders::_1,5);std::shared_ptrmyA(a,de

c++ - 使用带有 unique_ptr 的 union

当我尝试std::move或std::make_unique时,尝试在union中使用unique_ptr会给我一个段错误。#include#includeunionmyUnion{struct{std::unique_ptrupFloat;}structUpFloat;struct{std::unique_ptrupInt;}structUpInt;myUnion(){}~myUnion(){}};structmyStruct{intx;myUnionnum;};intmain(){myStructaStruct,bStruct;aStruct.x=1;bStruct.x=2;aut