[class.ctor] (original) (raw)
Constructors do not have names.
[ Example
:
struct S {
S();
};
S::S() { }
— end example
]
A constructor is used to initialize objects of its class type.
Because constructors do not have names, they are never found during 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.
[ Note
:
The syntax looks like an explicit call of the constructor.
— end note
]
[ Example
:
complex zz = complex(1,2.3); cprint( complex(7.8,1.2) );
— end example
]
[ Note
:
For initialization of objects of class type see [class.init].
— 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.
11.4.4.1 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 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:
- X is a union that has a variant member with a non-trivial default constructor and no variant member of X has a default member initializer,
- X is a non-union class that has a variant member Mwith a non-trivial default constructor and no variant member of the anonymous union containing Mhas a default member initializer,
- any non-static data member with no default member initializer ([class.mem]) is of reference type,
- any non-variant non-static data member of const-qualified type (or array thereof) with no brace-or-equal-initializeris not const-default-constructible ([dcl.init]),
- X is a union and all of its variant members are of const-qualified type (or array thereof),
- X is a non-union class and all members of any anonymous union member are of const-qualified type (or array thereof),
- any potentially constructed subobject, except for a non-static data member with a brace-or-equal-initializer, has class type M (or array thereof) and either Mhas no default constructor or overload resolution ([over.match]) as applied to find M's corresponding constructor results in an ambiguity or in a function that is deleted or inaccessible from the defaulted default constructor, or
- any potentially constructed subobject has a type with a destructor that is deleted or inaccessible from the defaulted default constructor.
A default constructor istrivialif it is not user-provided and if:
- its class has no virtual functions ([class.virtual]) and no virtual base classes ([class.mi]), and
- no non-static data member of its class has a default member initializer ([class.mem]), and
- all the direct base classes of its class have trivial default constructors, and
- for all the non-static data members of its class that are of class type (or array thereof), each such class has a trivial default constructor.
Otherwise, the default constructor isnon-trivial.
A default constructor that is defaulted and not defined as deleted isimplicitly definedwhen it is odr-used ([basic.def.odr]) to create an object of its class type ([intro.object]), when it is needed for constant evaluation ([expr.const]), or when it is explicitly defaulted after its first declaration.
The implicitly-defined default constructor performs the set of initializations of the class that would be performed by a user-written default constructor for that class with noctor-initializer and an emptycompound-statement.
If that user-written default constructor would be ill-formed, the program is ill-formed.
If that user-written default constructor would satisfy the requirements of a constexpr constructor ([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
:
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
]
A program is ill-formed if the default constructor for an object is implicitly used and the constructor is not accessible ([class.access]).
[ Note
:
[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.4.2 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
:
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
:
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
:
All forms of copy/move constructor may be declared for a class.
[ Example
:
struct X {
X(const X&);
X(X&);
X(X&&);
X(const X&&);
};
— end example
]
— end note
]
[ Note
:
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
:
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
:
struct S { template 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 form
X::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&.103
Otherwise, the implicitly-declared copy constructor will have the form
X::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
:
When the move constructor is not implicitly declared or explicitly supplied, expressions that otherwise would have invoked the move constructor may instead invoke a copy constructor.
— end note
]
The implicitly-declared move constructor for class X will have the form
X::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
:
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
:
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
:
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.