What is the default move construct?

老子叫甜甜 提交于 2021-02-17 03:30:36

问题


What is the definition of the default move constructor? I can't think of anything sensible. Maybe a swap on ptr members and copy on values/reference member?


回答1:


That's what the standard says (12.8/15):

The implicitly-defined copy/move constructor for a non-union class X performs a memberwise copy/move of its bases and members. [ Note: brace-or-equal-initializers of non-static data members are ignored. See also the example in 12.6.2. —end note ] The order of initialization is the same as the order of initialization of bases and members in a user-defined constructor (see 12.6.2). Let x be either the parameter of the constructor or, for the move constructor, an xvalue referring to the parameter. Each base or non-static data member is copied/moved in the manner appropriate to its type:

— if the member is an array, each element is direct-initialized with the corresponding subobject of x;

— if a member m has rvalue reference type T&&, it is direct-initialized with static_cast<T&&>(x.m);

— otherwise, the base or member is direct-initialized with the corresponding base or member of x.

Virtual base class subobjects shall be initialized only once by the implicitly-defined copy/move constructor (see 12.6.2).

For pointers and fundamental types moving is the same as copying.




回答2:


Non-union members are moved in their initialization order, using the move semantics defined for each member type.

http://en.cppreference.com/w/cpp/language/move_constructor



来源:https://stackoverflow.com/questions/18733227/what-is-the-default-move-construct

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!