[class.ctor] (original) (raw)

11 Classes [class]

11.4 Class members [class.mem]

11.4.5 Constructors [class.ctor]

11.4.5.1 General [class.ctor.general]

Constructors do not have names.

[Example 1: struct S { S(); }; S::S() { } — _end example_]

A constructor is used to initialize objects of its class type.

[Note 1:

Because constructors do not have names, they are never found during unqualified name lookup; however an explicit type conversion using the functional notation ([expr.type.conv]) will cause a constructor to be called to initialize an object.

The syntax looks like an explicit call of the constructor.

— _end note_]

[Example 2: complex zz = complex(1,2.3); cprint( complex(7.8,1.2) ); — _end example_]

[Note 2:

For initialization of objects of class type see [class.init].

— _end note_]

An object created in this way is unnamed.

[Note 4:

Explicit constructor calls do not yield lvalues, see [basic.lval].

— _end note_]

A constructor can be invoked for aconst,volatileorconst volatileobject.

constandvolatilesemantics ([dcl.type.cv]) are not applied on an object under construction.

They come into effect when the constructor for the most derived object ([intro.object]) ends.

The address of a constructor shall not be taken.

[Note 6:

A return statement in the body of a constructor cannot specify a return value ([stmt.return]).

— _end note_]

A constructor shall not be a coroutine.

A constructor shall not have an explicit object parameter ([dcl.fct]).

11.4.5.2 Default constructors [class.default.ctor]

A default constructor for a class Xis a constructor of class Xfor which each parameter that is not a function parameter pack has a default argument (including the case of a constructor with no parameters).

If there is no user-declared constructor or constructor template for classX, a non-explicit constructor having no parameters is implicitly declared as defaulted ([dcl.fct.def]).

An implicitly-declared default constructor is an inline public member of its class.

A defaulted default constructor for class X is defined as deleted if

A default constructor for a class X istrivialif it is not user-provided and if

Otherwise, the default constructor isnon-trivial.

If a default constructor of a union-like class X is trivial, then for each union Uthat is either X or an anonymous union member of X, if the first variant member, if any, of Uhas implicit-lifetime type ([basic.types.general]), the default constructor of X begins the lifetime of that member if it is not the active member of its union.

[Note 1:

It is already the active member if U was value-initialized.

— _end note_]

If that user-written default constructor would be ill-formed, the program is ill-formed.

If that user-written default constructor would be constexpr-suitable ([dcl.constexpr]), the implicitly-defined default constructor is constexpr.

Before the defaulted default constructor for a class is implicitly defined, all the non-user-provided default constructors for its base classes and its non-static data members are implicitly defined.

[Note 2:

An implicitly-declared default constructor has an exception specification ([except.spec]).

An explicitly-defaulted definition might have an implicit exception specification, see [dcl.fct.def].

— _end note_]

[Note 3:

A default constructor is implicitly invoked to initialize a class object when no initializer is specified ([dcl.init.general]).

Such a default constructor needs to be accessible ([class.access]).

— _end note_]

[Note 4:

[class.base.init] describes the order in which constructors for base classes and non-static data members are called and describes how arguments can be specified for the calls to these constructors.

— _end note_]

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 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&.89

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

[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:

[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

otherwise the copy/move constructor isnon-trivial.

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:

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.general]) 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.