草庐IT

c++ - 是否需要使用右值引用重复调用 move?

structBar{Bar(std::string&&val):m_Val(std::move(val)){}//ABar&operator=(Bar&&_other){m_Val=std::move(_other.m_Val);}std::stringm_Val;}structFoo{voidFunc1(Bar&¶m){Func2(std::move(param))//B}voidFunc2(Bar&¶m){m_Bar=std::move(param);//C}Barm_Bar;};voidmain(){Foof;std::strings="somestr";Barb

c++ - 在 move 构造函数中放弃常量是否可以接受?

假设我有一个Foo类,它有一个指向Bar的私有(private)指针:classFoo{private:Bar*bar;public:Foo():bar(newBar()){}~Foo(){deletebar;}};如果指针bar不应该被重新分配给不同的实例,那么让指针本身成为const以阻止我(或维护者)是有意义的以后不要这样做:private:Bar*constbar;只要有机会,我就喜欢这样做。如果我想写一个move构造函数,它看起来像这样:Foo(Foo&&f):bar(f.bar){f.bar=NULL;//uhoh;f.barisconst.}我可以通过放弃f.bar的常量

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++ - 在 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