草庐IT

nothrow_move_constructible

全部标签

c++ - 为什么在这种情况下调用非 const 右值 move 构造函数?

我看过相关的问题,他们主要讨论我们是否应该将const右值引用作为参数。但我仍然无法理解为什么在以下代码中调用了非常量move构造函数:#includeusingnamespacestd;classA{public:A(intconst&&i){cout 最佳答案 这里是你的代码的一个稍微修改的版本:#include#if0usingT=int;#elsestructT{T(int){}};#endifusingnamespacestd;classA{public:A(Tconst&&i){cout当T==int时,您会得到非常量重

c++ - 如何使用 move 的对象?

这个问题在这里已经有了答案:WhatcanIdowithamoved-fromobject?(2个答案)关闭6年前。move物体后,它必须是可破坏的:Tobj;func(std::move(obj));//don'tuseobjandletitbedestroyedasnormal但是obj还能做什么呢?你能把另一个物体移进去吗?Tobj;func(std::move(obj));obj=std::move(other);这取决于确切的类型吗?(例如,std::vector可以做出您不能对所有T都依赖的特定保证。)所有类型是否都支持除对移出对象的破坏之外的某些东西,这是否是必需的,甚至

c++ - 为什么 std::allocator::construct 和 std::allocator::destroy 在元素类型上模板化?

std::allocator的construct和destroy成员函数根据要构造的元素的类型进行参数化:templateclassallocator{public:typedefTvalue_type;typedefT*pointer;templatevoidconstruct(U*p,Args&&...args);templatevoiddestroy(U*p);...};这样做的理由是什么?他们为什么不选择value_type*或pointer?好像allocator应该只知道如何构造或销毁T类型的对象. 最佳答案 这与all

c++ - 在 move 构造函数中用作 arg 后,哪些 std 类型保证为空/空

我知道shared_ptr,unique_ptr,weak_ptr在相同类型的构造函数中用作RVR参数后保证为空,但是我想知道除了我提到的那些之外,标准是否为其他一些std::类型指定了这一点。请注意,我知道move后的元素处于有效但未指定状态,我在这里对指定了哪些类型状态感兴趣。 最佳答案 使移出对象处于“空”状态的类型是智能指针、锁([thread.lock.unique.cons]/21、[thread.lock.shared.cons]/21)、文件流([filebuf.cons]/(4.2))、future([future

C++11:值传递是否涉及 move 语义?

我有一个看起来像这样的API:voidWriteDefaultFileOutput(std::wostream&str,std::wstringtarget){//Somecodethatmodifiestargetbeforeprintingitandsuch...}我想知道通过这样做启用move语义是否明智:voidWriteDefaultFileOutput(std::wostream&str,std::wstring&&target){//Asabove}voidWriteDefaultFileOutput(std::wostream&str,std::wstringconst

c++ - move 语义和运算符重载

这与thisanswer有关由MatthieuM.提供,介绍如何通过+运算符重载使用move语义(通常,运算符不会直接重新分配回左参数)。他建议实现三个不同的重载:inlineToperator+(Tleft,Tconst&right){left+=right;returnleft;}inlineToperator+(Tconst&left,Tright){right+=left;returnright;}//commutativeinlineToperator+(Tleft,T&&right){left+=right;returnleft;}//disambiguation数字1和3

c++ - "error: use of deleted function"在 move 构造函数中对 unique_ptr 调用 std::move 时

#includeclassA{public:A(){}A(constA&&rhs){a=std::move(rhs.a);}private:std::unique_ptra;};此代码无法使用g++4.8.4编译并抛出以下错误:error:useofdeletedfunction‘std::unique_ptr&std::unique_ptr::operator=(conststd::unique_ptr&)[with_Tp=int;_Dp=std::default_delete]’a=std::move(rhs.a);^我知道unique_ptr的复制构造函数和复制赋值构造函数已删除

c++ - std::move 和 std::copy 是否相同?

我尝试做类似的事情:std::copy(std::make_move_iterator(s1.begin()),std::make_move_iterator(s1.end()),std::make_move_iterator(s2.begin()));出现这个错误:error:usingxvalue(rvaluereference)aslvalue*__result=std::move(*__first);这让我感到困惑。如果您使用std::move,也会发生同样的事情。看起来GCC内部使用了一个名为std::__copy_move_a的函数,它move而不是复制。使用std::co

c++ - 为什么这里调用复制构造函数,而不是 move 构造函数?

我有一个类,PlayerInputComponent:.h:classPlayerInputComponent{public:PlayerInputComponent(PlayerMoveComponent&parentMoveComponent_,std::unique_ptrinputConverter_);PlayerInputComponent(PlayerInputComponent&&moveFrom);voidupdate();private:std::unique_ptrinputConverter;PlayerMoveComponent&parentMoveCompo

c++ - 子类中的默认 move 构造函数

在C++11中,如果基类定义了自己的move(复制)构造函数(赋值运算符),子类是否需要在调用基类的地方定义自己的move(复制)构造函数(赋值运算符)显式调用相应的构造函数/运算符?每次都清楚地定义构造函数、析构函数、move/复制构造函数(赋值运算符)是个好主意吗?structBase{Base(){}Base(Base&&o);};structSub:publicBase{Sub(Sub&&o);//NeedIdoitexplicitly?Ifnot,whatthecompilerwilldoforme}; 最佳答案 如果您没