草庐IT

make_scoped

全部标签

c++ - 带有大括号初始化的 make_unique

https://en.cppreference.com/w/cpp/memory/unique_ptr/make_unique写道std::make_unique可以实现为templatestd::unique_ptrmake_unique(Args&&...args){returnstd::unique_ptr(newT(std::forward(args)...));}这不适用于没有构造函数的普通结构。这些可以用大括号初始化,但没有非默认构造函数。示例:#includestructpoint{intx,z;};intmain(){std::make_unique(1,2);}Com

c++ - 为什么 make_unique 不能与 unique_ptr::reset 一起使用?

我尝试用VS2013编译一些C++代码,unique_ptr::reset()似乎不适用于make_unique();一个小的可编译重现代码片段如下:#includeusingnamespacestd;intmain(){unique_ptrp=make_unique(3);p.reset(make_unique(10));}从命令行编译:C:\Temp\CppTests>cl/EHsc/W4/nologotest.cpp这些是来自MSVC编译器的错误:test.cpp(6):errorC2280:'voidstd::unique_ptr>::reset>>(_Ptr2)':attem

C++:为什么这个简单的 Scope Guard 有效?

到目前为止,每个看过的作用域守卫都有一个守卫bool变量。例如,请参阅此讨论:Thesimplestandneatestc++11ScopeGuard但是一个简单的守卫可以工作(gcc4.9,clang3.6.0):templatestructfinally_t:publicC{finally_t(C&&c):C(c){}~finally_t(){(*this)();}};templatestaticfinally_tfinally_create(C&&c){returnstd::forward(c);}#defineFINCAT_(a,b)a##b#defineFINCAT(a,b)

c++ - 为什么 allocate_shared 和 make_shared 这么慢

我刚刚编写了一个测试程序来找到分配和释放许多由shared_ptr管理的对象的最快方法。我尝试了shared_ptr和new,shared_ptr和pool,make_shared,allocate_shared。让我惊讶的是allocate_shared比shared_ptr和pool慢。我使用发布版本测试vs2017+win10中的代码。发布build设置为默认(/O2)。我还在gcc4.8.5+centos6.2中使用g++-std=c++11-O3对其进行了测试。代码是:#include#include#include#include#include#includeusingn

c++ - C++ 11 中 boost::scoped_ptr 的替代方案

我们刚刚将编译器升级到支持C++11的VC++2013。之前我们一直在使用来自Boost的shared_ptr和scoped_ptr类,但由于这是我们一直在使用的Boost类,我们正在寻找删除该依赖项。据我所知,std::shared_ptrs是boost::shared_ptrs的直接替代品,所以这(希望)很容易。但是,Boostscoped_ptrs的最佳替代品是什么(如果有的话)?会是unique_ptr吗?(老实说,虽然我写了代码,但那是大约10年前的事了,我已经忘记了使用scoped_ptrs的目的是什么......也许我只是在“玩”Boost,但到目前为止正如我所看到的,在

c++ - 如何让 boost::make_shared 成为我类(class)的 friend

我编写了一个带有protected构造函数的类,因此只能使用静态create()函数生成新实例,该函数将shared_ptr返回我的类。为了提供有效的分配,我想在create函数中使用boost::make_shared,但是编译器提示说我的类构造函数在boost::make_shared中受到保护。我决定让我的boost::make_shared成为我类的friend,但我对语法感到困惑。我试过了templatefriendboost::shared_ptrboost::make_shared(constConnectionManagerPtr&,conststd::string&)

c++ - "single allocation"对 boost::make_shared 意味着什么

在make_shared的boost文档中,它说:Besidesconvenienceandstyle,suchafunctionisalsoexceptionsafeandconsiderablyfasterbecauseitcanuseasingleallocationforboththeobjectanditscorrespondingcontrolblock,eliminatingasignificantportionofshared_ptr'sconstructionoverhead.我不明白“单一分配”的意思,是什么意思? 最佳答案

c++ - VS2010 : "Cannot find the resource compiler DLL. Please make sure the path is correct."

我一直在关注theForger'swin32APItutorial,然后我决定打开menu_one.rc文件以从VS2010中查看其内容,但我收到此错误:我已经仔细检查了这两个位置,但实际上这两个文件都不存在。我仅有的rcdll.dll拷贝位于:C:\ProgramFiles(x86)\MicrosoftSDKs\Windows\v7.0A\Bin\rcdll.dllandC:\ProgramFiles(x86)\MicrosoftSDKs\Windows\v7.0A\Bin\x64\rcdll.dll我试过做repairinstalloftheWindowsSDK,但这并没有解决问题

c++ - 使用 make_unique 语句重新分配 unique_ptr 对象 - 内存泄漏?

我不明白下面的语句会做什么(特别是第二行)?autobuff=std::make_unique(128);buff=std::make_unique(512);第二次调用make_unique后跟赋值运算符会释放第一次调用分配的内存,还是会发生内存泄漏?我必须使用buff.reset(newint[512]);吗?我调试了它,但没有发现任何operator=被调用,也没有发现任何析构函数被调用(通过unique_ptr)。 最佳答案 移动赋值运算符被调用,它执行if(this!=&_Right){//different,dothes

c++ - scoped_ptr 所有权

这个问题在这里已经有了答案:关闭9年前。PossibleDuplicate:WhatisasmartpointerandwhenshouldIuseone?我正在阅读anarticle我找到了一个小例子来演示boost::scoped_ptr的使用:#include#include#include#includestaticintcount=0;classprinter{intm_id;public:printer(void):m_id(count++){}~printer(void){std::coutp1(newprinter);boost::scoped_ptrp2(newpri