草庐IT

move_computer

全部标签

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++ - 为什么不能 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++ - 最佳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,这部分我不明白。或者我在编程中

c++ - 制作一个 const unique_ptr 然后尝试从它 std::move 给出相同的错误,就好像您试图访问复制构造函数一样

当我们尝试复制unique_ptr(例如,将一个唯一指针分配给另一个)时,我注意到错误是ErrorC2280std::unique_ptr#includeintmain(){std::unique_ptra=std::make_unique(2);std::unique_ptrb=a;}没关系,因为unique_ptr没有定义复制构造函数。您不会从唯一指针进行复制以在它们之间move(转移指针的所有权)。有趣的是(好吧,也许不是),这段代码抛出了同样的错误。现在我知道它是无效的(我将第一个unique_ptr声明为不可变对象(immutable对象)),但错误消息暗示它正在尝试调用复制

c++ - 复制初始化: why move or copy constructor was not called even if copy-elision is turned off?

我的问题不同,因为我可能“知道”复制省略。我正在学习复制初始化。但是,以下代码让我感到困惑,因为我已经使用-fno-elide-contructors-O0选项关闭了复制省略。#includeusingnamespacestd;classtest{public:test(inta_,intb_):a{a_},b{b_}{}test(consttest&other){cout我首先使用命令构建:g++-std=c++11-fno-elide-constructors-O0main.cpp-omain得到如下结果:**showelideconstructors**moveconstruct