Files
2025-10-25 03:02:53 +03:00

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]

1

#

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]

2

#

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]

3

#

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]

4

#

[Note 5:

Some language constructs have special semantics when used during construction; see [class.base.init] and [class.cdtor].

— end note]

5

#

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.

6

#

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]

7

#

A constructor shall not be a coroutine.

8

#

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