c++ - What are the rules for automatic generation of move operations? -
c++ - What are the rules for automatic generation of move operations? -
in c++98, c++ compiler automatically generate re-create constructor , re-create assignment operator via member-wise copy, e.g.
struct x { std::string s; std::vector<int> v; int n; };
the compiler automatically generates re-create constructor , re-create assignment operator x
, using member-wise copy.
but how things alter in c++11 move semantics?
are move constructor , move assignment operator automatically generated, re-create constructors , re-create assignment operators?
are there cases in move operations not automatically generated?
from standard ch. 12 - special fellow member functions
par 12.8 copying , moving class objects (emphasis mine)
9 . if definition of class x not explicitly declare move constructor, one implicitly declared defaulted if , if
— x not have user-declared re-create constructor,
— x not have user-declared re-create assignment operator,
— x not have user-declared move assignment operator, and
— x not have user-declared destructor.
[ note: when move constructor not implicitly declared or explicitly supplied, expressions otherwise have invoked move constructor may instead invoke re-create constructor. —end note ]
then 11
explains rules deleting defaulted move constructor
11 . implicitly-declared copy/move constructor inline public fellow member of class. a defaulted copy/ move constructor class x defined deleted (8.4.3) if x has:
— variant fellow member non-trivial corresponding constructor , x union-like class,
— non-static info fellow member of class type m (or array thereof) cannot copied/moved because overload resolution (13.3), applied m’s corresponding constructor, results in ambiguity or function deleted or inaccessible defaulted constructor,
— direct or virtual base of operations class b cannot copied/moved because overload resolution (13.3), applied b’s corresponding constructor, results in ambiguity or function deleted or inaccessible defaulted constructor,
— direct or virtual base of operations class or non-static info fellow member of type destructor deleted or inaccessible defaulted constructor, or,
— re-create constructor, non-static info fellow member of rvalue reference type. defaulted move constructor defined deleted ignored overload resolution (13.3, 13.4).
[ note: deleted move constructor otherwise interfere initialization rvalue can utilize re-create constructor instead. —end note ]
on complexity of *the rules can overwhelming. it's utilize technique bypass complexity. examples :
make utilize of rule of zero simplify writing of bulk of classes. (on implicitly deleted) explicitly default special fellow member function in question; if have been implicitly defined deleted, compiler complain.* points made in comments myself (1) , dyp (2)
c++ c++11 move-semantics
Comments
Post a Comment