草庐IT

c++ - move 类数据成员 (C++)

我想知道我这样做是否正确。我有一个包含一些数据的类:classFoo{//...Typea_;Typeb_;Typec_;};还有一个做其他事情的不同类,但使用classFoo构造。所以,我想像这样声明一个ctor:classBar{Typea_;Typeb_;Typec_;AnotherTypeA_;AnotherTypeB_;//...public:typedefstd::tupleTuple;Bar(constTuple&);Bar(Tuple&&);};我现在需要创建一个Foo方法,它将返回Bar需要的数据成员元组,我可以将其传递给Bar的导演。我还为Tuple创建了一个右值引

c++ - 如何将对象 move 到未初始化的内存中?

给定一个已分配但未初始化的内存位置,我如何将一些对象move到该位置(破坏原始位置),而不构造可能昂贵的中间对象? 最佳答案 您可以使用placementnew在内存中move构造它:void*memory=get_some_memory();Thing*new_thing=new(memory)Thing(std::move(old_thing));如果它有一个非平凡的析构函数,那么你需要在完成后显式地销毁它:new_thing->~Thing(); 关于c++-如何将对象move到未

c++ - 右值引用和文字

考虑代码templatevoidfoo(Args&&...){}templatevoidbar(Args&&...args){foo(std::forward(args)...);}intmain(){bar(true);}~gcc4.7.2给出错误error:nomatchingfunctionforcallto‘forward(bool&)’note:candidatesare:templateconstexpr_Tp&&std::forward(typenamestd::remove_reference::type&)note:templateargumentdeduction/

c++ - 为什么不能 move 这些变量?

C++move语义中似乎错失了很多机会。我想了解这些背后的基本原理,以及为什么标准在定义以下情况下何时应move变量时没有更积极地定义:stringf(){strings;returns+"";}这调用了operator+(conststring&,constchar*),而不是operator+(string&&,constchar*),我相信是因为s是一个左值。难道标准不能说,在函数中最后一次使用局部变量时,该变量被认为是可move的吗?我觉得有点类似的例子:structA{A(string&&);};stringg(){strings;returns;//sismoved}Ah(

c++ - 如何改进 std::vector 参数传递( move 语义?)

我似乎无法完全理解move语义:我想从外部函数填充std::vector(类的成员)。目前,我有类似的东西:voidfillVector(MyClass&myclass){std::vectorvec;/*Fillingvec*///...myclass.setVector(vec);}classMyClass{public:setVector(conststd::vector&v){v_=v;}private:std::vectorv_;};intmain(){MyClassmyclass;fillVector(myclass);/*Usemyclass.v_somehow*/.}我

c++ - 为什么是 "move semantics"而不是简单的 memcpy?

给定以下代码:typenamestd::aligned_storage::typestorage_t;//thismovesthebackofsrctothebackofdst:voidpush_popped(std::list&dstLst,std::list&srcLst){auto&src=srcLst.back();dstLst.push_back(storage_t());auto&dst=dstLst.back();std::memcpy(&dst,&src,sizeof(T));srcLst.pop_back();}我知道这种方法通常不正确的3个原因(即使它避免调用src

c++ - 我怎样才能简单地使用无法识别的字符?

多亏了BoostSpirit库,我设法解析了一个pgn文件,但是一旦出现一些我没有“预料到”的字符,它就会失败。这是我的Spirit语法:#include#include#includeBOOST_FUSION_ADAPT_STRUCT(loloof64::pgn_tag,(std::string,key),(std::string,value))BOOST_FUSION_ADAPT_STRUCT(loloof64::game_move,(unsigned,move_number),(std::string,move_turn),(std::string,white_move),(st

c++ - 最佳C++ move 构造函数实现实践

我想了解move构造函数的实现。我们都知道如果我们需要在C++类中管理资源,我们需要实现五法则(C++编程)。微软给了我们一个例子:https://msdn.microsoft.com/en-us/library/dd293665.aspx这是一个更好的方法,它使用复制交换来避免代码重复:Dynamicallyallocatinganarrayofobjects//C++11A(A&&src)noexcept:mSize(0),mArray(NULL){//Canwewritesrc.swap(*this);//or(*this).swap(src);(*this)=std::move

c++ - sort() 是否自动使用 move 语义?

isocpp.org指出:move-basedstd::sort()andstd::set::insert()havebeenmeasuredtobe15timesfasterthancopybasedversions[...]ifyourtypehasamoveoperation,yougaintheperformancebenefitsautomaticallyfromthestandardalgorithms.这是否意味着如果您在没有move构造函数或move赋值运算符的用户定义类型上调用sort(),那么就没有使用move语义?换句话说,要获得C++11性能改进的诸多好处,您应

c++ - 同一对象的双 move 是从左向右复制吗?

我只是c++11中move操作的初学者,所以玩它。但是发现了一些我无法理解的东西。#includeusingnamespacestd;classA{public:A(){coutCORRECT!!coutCORRECT!!b=std::move(a);//idon'tknowmaybesillybutstillletsdoitWHYNOT!!!,couldbejustmistake??coutWRONG!!coutWRONG!!}我原以为b=std::move(a)操作会有所不同,因为我第二次在对象a上应用move,但它正在向左复制侧面对象b到右侧对象a,这部分我不明白。或者我在编程中