草庐IT

auto_ptr_ref

全部标签

c++ - 如何用元组转发unique_ptr?

想象一个函数期望对std::unique_ptr的右值引用。voidfoo(std::unique_ptr&&a);在我的真实示例中,有不止一个参数,因此我决定使用std::tuple将参数转发给它右值引用-所以std::forward_as_tuple:voidforward_to_foo(std::tuple&&>&&t);intmain(){forward_to_foo(std::forward_as_tuple(std::make_unique(8)));}目前一切正常当我想“解压”这个元组时出现问题voidforward_to_foo(std::tuple&&>&&t){fo

c++ - auto stdMaxInt = std::max<int> 的类型推导失败;

使用GCC4.8.4和g++--std=c++11main.cpp输出以下errorerror:unabletodeduce‘auto’from‘max’autostdMaxInt=std::max;对于这段代码#includetemplateconstT&myMax(constT&a,constT&b){return(a;myMaxInt(1,2);autostdMaxInt=std::max;stdMaxInt(1,2);}为什么它适用于myMax但不适用于std::max?我们可以让它与std::max一起工作吗? 最佳答案

c++ - 使用 reset 初始化 unique_ptr 是个坏习惯吗?

当我需要一个类型为std::unique_ptr的数据成员时,那么我通常使用std::unique::reset()初始化这个unique_ptr用一个新的对象。下面是一个简化的例子:classA{public:voidSetValue(intx){data_.reset(newB(x));}private:std::unique_ptrdata_;};在代码审查中,一位审查者提到这是一个坏习惯,他让我不要使用reset()。如果可能的话。相反,他建议使用以下方法:std::make_unique或者像下面这样的模板函数:templatestructMakeUniqueResult{u

C++ 重载解析、用户定义转换和函数模板

对于g++3.4和4.7,我观察到以下奇怪的行为:如果需要用户定义的转换,则函数模板不匹配,而普通函数会。我在C++98标准中找不到相应的规则。g++是否正确(正如我假设的那样)?或者这是一个错误?templateintx(auto_ptr_refp){return1;}//thiswouldmatch/*intx(auto_ptr_refp){return2;}*/voiddummy(){cout()) 最佳答案 海湾合作委员会是正确的,templateargumentdeduction不考虑隐式转换。Typedeductiond

c++ - constexpr if with initializer 由标准保证吗? 'constexpr(constexpr auto x = f(); x) { }'

我找不到任何关于新C++17if初始化语法的信息和“constexprif”在:http://open-std.org/JTC1/SC22/WG21/docs/papers/2016/p0128r1.html不过,Clang-HEAD支持该语法...constexprautof(){returntrue;}intmain(){ifconstexpr(constexprautox=f();x){}}在线代码在这里->http://melpon.org/wandbox/permlink/dj3a9ChvjhlNc8nr是constexprif带有标准保证的初始值设定项,如constexpr

c++ - unique_ptr 删除器开销

在正常的C++设计中,大多数对象都可以通过delete删除。声明,free函数,或库特定的等效于free.对于此类对象,unique_ptrDeleter实现可以是通过空基类优化消除的无状态对象。但是,某些库需要使用另一个对象(可能包含函数指针或其他上下文)从该库中删除对象。typedefstructlib_objectlib_object;structlib_api{lib_object(*createInstance)();void(*freeInstance)(lib_object*o);};可以将其包装在unique_ptr中通过存储lib_api作为自定义数据成员的指针Del

c++ - Pimpl with unique_ptr : Why do I have to move definition of constructor of interface to ".cpp"?

只要我不将构造函数(B)的定义移动到标题B.h中,代码就可以工作。B.hclassImp;//imp;B();//B.cpp#include"B.h"#include"Imp.h"B::B(){}~B::B(){}Imp.hclassImp{};Main.cpp(编译我)#include"B.h"Error:deletionofpointertoincompletetypeError:useofundefinedtype'Imp'C2027我能以某种方式理解必须将析构函数移动到.cpp,因为可能会调用Imp的解构:-deletepointer-of-Imp;//somethinglik

c++ - 错误 : ‘template<class> class std::auto_ptr’ is deprecated

我正在使用scons和ubuntu。当我使用'scons'制作一些程序时,会发生错误,例如,src/db/DBTextLoader.cc:296:3:error:‘templateclassstd::auto_ptr’isdeprecated[-Werror=deprecated-declarations]/usr/include/c++/5/bits/unique_ptr.h:49:28:note:declaredheretemplateclassauto_ptr;这是我的命令;$./configuer$sourcesomething.sh$scons其实我也不知道。我已经在搜索这个

c++ - 将带有 unique_ptr 的可变 lambda 传递给 const& std::function

我有一个调度函数,它在主线程中执行给定的lambda。为了这个问题,假设它看起来像下面这样:voiddispatch(conststd::function&fn){fn();}我需要在不中断主线程的情况下在新线程中加载新对象。所以我执行以下操作:1)启动一个新线程并在线程内创建一个新的唯一指针,2)调用dispatch并将新的唯一指针传播到它所属的位置。std::unique_ptrfoo;//nullptr//dotheloadinginanewthread:std::threadt([&](){//inthenewthread,loadnewvalue"Blah"andstorei

c++ - boost::shared_ptr 标准容器

假设我有一个foo类,并希望使用std::map来存储一些boost::shared_ptrs,例如:classfoo;typedefboost::shared_ptrfoo_sp;typededstd::mapfoo_sp_map;foo_sp_mapm;如果我向map添加一个新的foo_sp但使用的键已经存在,现有的条目是否会被删除?例如:foo_sp_mapm;voidfunc1(){foo_spp(newfoo);m[0]=p;}voidfunc2(){foo_spp2(newfoo);m[0]=p2;}原来的指针(p)被p2替换后会不会被释放?我很确定会这样,但我认为值得询问