[class.copy.ctor] (original) (raw)
11 Classes [class]
11.4 Class members [class.mem]
11.4.5 Constructors [class.ctor]
11.4.5.3 Copy/move constructors [class.copy.ctor]
A non-template constructor for classXis a copy constructor if its first parameter is of typeX&,const X&,volatile X&orconst volatile X&, and either there are no other parameters or else all other parameters have default arguments ([dcl.fct.default]).
[Example 1:
X::X(const X&)andX::X(X&,int=1)are copy constructors.
struct X { X(int); X(const X&, int = 1);}; X a(1); X b(a, 0); X c = b; — _end example_]
A non-template constructor for class X is a move constructor if its first parameter is of type X&&, const X&&,volatile X&&, or const volatile X&&, and either there are no other parameters or else all other parameters have default arguments ([dcl.fct.default]).
[Example 2:
Y::Y(Y&&) is a move constructor.
struct Y { Y(const Y&); Y(Y&&);};extern Y f(int); Y d(f(1)); Y e = d; — _end example_]
[Note 1:
All forms of copy/move constructor can be declared for a class.
[Example 3: struct X { X(const X&); X(X&); X(X&&); X(const X&&); }; — _end example_]
— _end note_]
[Note 2:
If a classXonly has a copy constructor with a parameter of typeX&, an initializer of typeconst Xorvolatile Xcannot initialize an object of typecv X.
[Example 4: struct X { X(); X(X&); };const X cx; X x = cx; — _end example_]
— _end note_]
A declaration of a constructor for a classXis ill-formed if its first parameter is of typecv Xand either there are no other parameters or else all other parameters have default arguments.
A member function template is never instantiated to produce such a constructor signature.
[Example 5: struct S { template<typename T> S(T); S();}; S g;void h() { S a(g); } — _end example_]
If the class definition does not explicitly declare a copy constructor, a non-explicit one is declared implicitly.
If the class definition declares a move constructor or move assignment operator, the implicitly declared copy constructor is defined as deleted; otherwise, it is defined as defaulted ([dcl.fct.def]).
The latter case is deprecated if the class has a user-declared copy assignment operator or a user-declared destructor ([depr.impldec]).
The implicitly-declared copy constructor for a classXwill have the formX::X(const X&) if each potentially constructed subobject of a class typeM(or array thereof) has a copy constructor whose first parameter is of typeconst M&orconst volatile M&.107
Otherwise, the implicitly-declared copy constructor will have the formX::X(X&)
If the definition of a class X does not explicitly declare a move constructor, a non-explicit one will be implicitly declared as defaulted if and only if
- X does not have a user-declared copy constructor,
- X does not have a user-declared copy assignment operator,
- X does not have a user-declared move assignment operator, and
- X does not have a user-declared destructor.
[Note 3:
When the move constructor is not implicitly declared or explicitly supplied, expressions that otherwise would have invoked the move constructor might instead invoke a copy constructor.
— _end note_]
The implicitly-declared move constructor for class X will have the formX::X(X&&)
An implicitly-declared copy/move constructor is an inline public member of its class.
A defaulted copy/move constructor for a classX is defined as deleted ([dcl.fct.def.delete]) if X has:
- a potentially constructed subobject typeM (or array thereof) that cannot be copied/moved because overload resolution ([over.match]), as applied to findM's corresponding constructor, results in an ambiguity or a function that is deleted or inaccessible from the defaulted constructor,
- a variant member whose corresponding constructor as selected by overload resolution is non-trivial,
- any potentially constructed subobject of a type with a destructor that is deleted or inaccessible from the defaulted constructor, or,
- for the copy constructor, a non-static data member of rvalue reference type.
[Note 4:
A defaulted move constructor that is defined as deleted is ignored by overload resolution ([over.match], [over.over]).
Such a constructor would otherwise interfere with initialization from an rvalue which can use the copy constructor instead.
— _end note_]
A copy/move constructor for classXis trivial if it is not user-provided and if:
- classXhas no virtual functions ([class.virtual]) and no virtual base classes ([class.mi]), and
- the constructor selected to copy/move each direct base class subobject is trivial, and
- for each non-static data member ofXthat is of class type (or array thereof), the constructor selected to copy/move that member is trivial;
otherwise the copy/move constructor isnon-trivial.
A copy/move constructor that is defaulted and not defined as deleted isimplicitly definedwhen it is odr-used ([basic.def.odr]), when it is needed for constant evaluation ([expr.const]), or when it is explicitly defaulted after its first declaration.
If the implicitly-defined constructor would satisfy the requirements of a constexpr constructor ([dcl.constexpr]), the implicitly-defined constructor is constexpr.
Before the defaulted copy/move constructor for a class is implicitly defined, all non-user-provided copy/move constructors for its potentially constructed subobjects are implicitly defined.
[Note 6:
An implicitly-declared copy/move constructor has an implied exception specification ([except.spec]).
— _end note_]
The implicitly-defined copy/move constructor for a non-union classXperforms a memberwise copy/move of its bases and members.
[Note 7:
Default member initializers of non-static data members are ignored.
— _end note_]
The order of initialization is the same as the order of initialization of bases and members in a user-defined constructor (see [class.base.init]).
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 withstatic_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 [class.base.init]).
The implicitly-defined copy/move constructor for a unionX copies the object representation ([basic.types]) of X.
For each object nested within ([intro.object]) the object that is the source of the copy, a corresponding object o nested within the destination is identified (if the object is a subobject) or created (otherwise), and the lifetime of o begins before the copy is performed.