6.6 KiB
[class.ctor.general]
11 Classes [class]
11.4 Class members [class.mem]
11.4.5 Constructors [class.ctor]
11.4.5.1 General [class.ctor.general]
A declarator declares a constructor if it is a function declarator ([dcl.fct]) of the form
ptr-declarator ( parameter-declaration-clause ) noexcept-specifieropt attribute-specifier-seqopt
where the ptr-declarator consists solely of anid-expression, an optional attribute-specifier-seq, and optional surrounding parentheses, and the id-expression has one of the following forms:
in a friend declaration ([class.friend]), the id-expression is a qualified-id that names a constructor ([class.qual]);
otherwise, in a member-declaration that belongs to themember-specification of a class or class template, the id-expression is the injected-class-name ([class.pre]) of the immediately-enclosing entity;
otherwise, theid-expression is a qualified-id whose unqualified-id is the injected-class-name of its lookup context.
Constructors do not have names.
In a constructor declaration, each decl-specifier in the optionaldecl-specifier-seq shall befriend,inline,constexpr,consteval, or an explicit-specifier.
[Example 1: struct S { S(); // declares the constructor};
S::S() { } // defines the constructor â 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 3:
[class.temporary] describes the lifetime of temporary objects.
â end note]
[Note 4:
Explicit constructor calls do not yield lvalues, see [basic.lval].
â end note]
[Note 5:
Some language constructs have special semantics when used during construction; see [class.base.init] and [class.cdtor].
â end note]
A constructor can be invoked for aconst,volatile orconstvolatile object.
const andvolatile semantics ([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]).