[class.mem] (original) (raw)

11 Classes [class]

11.4 Class members [class.mem]

member-specification: member-declaration member-specification access-specifier : member-specification

member-declaration: attribute-specifier-seq decl-specifier-seq member-declarator-list ; function-definition using-declaration using-enum-declaration static_assert-declaration template-declaration explicit-specialization deduction-guide alias-declaration opaque-enum-declaration empty-declaration

member-declarator-list: member-declarator member-declarator-list , member-declarator

member-declarator: declarator virt-specifier-seq pure-specifier declarator requires-clause declarator brace-or-equal-initializer identifier attribute-specifier-seq : constant-expression brace-or-equal-initializer

virt-specifier-seq: virt-specifier virt-specifier-seq virt-specifier

virt-specifier: override final

pure-specifier: = 0

The member-specification in a class definition declares the full set of members of the class; no member can be added elsewhere.

A direct member of a class X is a member of Xthat was first declared within the member-specification of X, including anonymous union objects ([class.union.anon]) and direct members thereof.

Members of a class are data members, member functions, nested types, enumerators, and member templates and specializations thereof.

[ Note

:

A specialization of a static data member template is a static data member.

A specialization of a member function template is a member function.

A specialization of a member class template is a nested class.

end note

]

For any other member-declaration, each declared entity that is not an unnamed bit-fieldis a member of the class, and each such member-declarationshall either declare at least one member name of the class or declare at least one unnamed bit-field.

A data member is a non-function member introduced by amember-declarator.

A member function is a member that is a function.

The enumerators of an unscoped enumeration defined in the class are members of the class.

A data member or member function may be declared static in its member-declaration, in which case it is a static member (see [class.static]) (a static data member ([class.static.data]) orstatic member function ([class.static.mfct]), respectively) of the class.

Any other data member or member function is a non-static member(a non-static data member ornon-static member function ([class.mfct.non-static]), respectively).

[ Note

:

A non-static data member of non-reference type is a member subobject of a class object.

end note

]

A member shall not be declared twice in themember-specification, except that

[ Note

:

A single name can denote several member functions provided their types are sufficiently different ([over.load]).

end note

]

[ Note

:

A complete-class context of a nested class is also a complete-class context of any enclosing class, if the nested class is defined within the member-specification of the enclosing class.

end note

]

A class is considered a completely-defined object type ([basic.types]) (or complete type) at the closing } of the class-specifier.

The class is regarded as complete within its complete-class contexts; otherwise it is regarded as incomplete within its own classmember-specification.

[ Example

:

struct S { using T = void(); T * p = 0;
virtual T f = 0;
};

end example

]

[ Example

:

int a; const int b = 0; struct S { int x1 : 8 = 42;
int x2 : 8 { 42 };
int y1 : true ? 8 : a = 42;
int y2 : true ? 8 : b = 42;
int y3 : (true ? 8 : b) = 42; int z : 1 || new int { 0 };
};

end example

]

A brace-or-equal-initializer for a non-static data memberspecifies a default member initializer for the member, and shall not directly or indirectly cause the implicit definition of a defaulted default constructor for the enclosing class or the exception specification of that constructor.

Within a class definition, a member shall not be declared with the thread_­local storage-class-specifier unless also declared static.

Apure-specifier shall be used only in the declaration of avirtual functionthat is not a friend declaration.

The optional attribute-specifier-seq in a member-declarationappertains to each of the entities declared by the member-declarators; it shall not appear if the optional member-declarator-list is omitted.

A virt-specifier-seq shall contain at most one of eachvirt-specifier.

A virt-specifier-seqshall appear only in the first declaration of a virtual member function ([class.virtual]).

The type of a non-static data member shall not be an incomplete type ([basic.types]), an abstract class type ([class.abstract]), or a (possibly multi-dimensional) array thereof.

[ Note

:

In particular, a class C cannot contain a non-static member of class C, but it can contain a pointer or reference to an object of class C.

end note

]

[ Note

:

See [expr.prim.id] for restrictions on the use of non-static data members and non-static member functions.

end note

]

[ Note

:

The type of a non-static member function is an ordinary function type, and the type of a non-static data member is an ordinary object type.

There are no special member function types or data member types.

end note

]

[ Example

:

A simple example of a class definition is

struct tnode { char tword[20]; int count; tnode* left; tnode* right; };

which contains an array of twenty characters, an integer, and two pointers to objects of the same type.

Once this definition has been given, the declaration

tnode s, *sp;

declares s to be a tnode and sp to be a pointer to a tnode.

With these declarations, sp->count refers to the count member of the object to which sp points;s.left refers to the left subtree pointer of the objects; and s.right->tword[0] refers to the initial character of the tword member of the right subtree of s.

end example

]

[ Note

:

Non-static data members of a (non-union) class with the same access control and non-zero size ([intro.object]) are allocated so that later members have higher addresses within a class object.

The order of allocation of non-static data members with different access control is unspecified.

Implementation alignment requirements might cause two adjacent members not to be allocated immediately after each other; so might requirements for space for managing virtual functions ([class.virtual]) and virtual base classes ([class.mi]).

end note

]

If T is the name of a class, then each of the following shall have a name different from T:

In addition, if class T has a user-declaredconstructor, every non-static data member of classT shall have a name different from T.

The common initial sequence of two standard-layout struct ([class.prop]) types is the longest sequence of non-static data members and bit-fields in declaration order, starting with the first such entity in each of the structs, such that corresponding entities have layout-compatible types, either both entities are declared with the no_­unique_­address attribute ([dcl.attr.nouniqueaddr]) or neither is, and either both entities are bit-fields with the same width or neither is a bit-field.

[ Example

:

struct A { int a; char b; }; struct B { const int b1; volatile char b2; }; struct C { int c; unsigned : 0; char b; }; struct D { int d; char b : 4; }; struct E { unsigned int e; char b; };

The common initial sequence of A and B comprises all members of either class.

The common initial sequence of A and C and of A and D comprises the first member in each case.

The common initial sequence of A and E is empty.

end example

]

Two standard-layout struct ([class.prop]) types arelayout-compatible classes if their common initial sequence comprises all members and bit-fields of both classes ([basic.types]).

Two standard-layout unions are layout-compatible if they have the same number of non-static data members and corresponding non-static data members (in any order) have layout-compatible types.

In a standard-layout union with an active memberof struct type T1, it is permitted to read a non-static data member m of another union member of struct type T2provided m is part of the common initial sequence of T1 and T2; the behavior is as if the corresponding member of T1 were nominated.

[ Example

:

struct T1 { int a, b; }; struct T2 { int c; double d; }; union U { T1 t1; T2 t2; }; int f() { U u = { { 1, 2 } };
return u.t2.c;
}

end example

]

[ Note

:

Reading a volatile object through a glvalue of non-volatile type has undefined behavior ([dcl.type.cv]).

end note

]

If a standard-layout class object has any non-static data members, its address is the same as the address of its first non-static data member if that member is not a bit-field.

Its address is also the same as the address of each of its base class subobjects.

[ Note

:

There might therefore be unnamed padding within a standard-layout struct object inserted by an implementation, but not at its beginning, as necessary to achieve appropriate alignment.

end note

]

11.4.1 Member functions [class.mfct]

A member function may be defined in its class definition, in which case it is an inline ([dcl.inline]) member function if it is attached to the global module, or it may be defined outside of its class definition if it has already been declared but not defined in its class definition.

[ Note

:

A member function is also inline if it is declaredinline, constexpr, or consteval.

end note

]

A member function definition that appears outside of the class definition shall appear in a namespace scope enclosing the class definition.

Except for member function definitions that appear outside of a class definition, and except for explicit specializations of member functions of class templates and member function templates ([temp.spec]) appearing outside of the class definition, a member function shall not be redeclared.

[ Note

:

There can be at most one definition of a non-inline member function in a program.

There may be more than one inline member function definition in a program.

end note

]

[ Note

:

Member functions of a class have the linkage of the name of the class.

end note

]

If the definition of a member function is lexically outside its class definition, the member function name shall be qualified by its class name using the ​::​ operator.

[ Example

:

struct X { typedef int T; static T count; void f(T); }; void X::f(T t = count) { }

The member function f of class X is defined in global scope; the notation X​::​f specifies that the function fis a member of class X and in the scope of class X.

In the function definition, the parameter type T refers to the typedef member T declared in class X and the default argument count refers to the static data member countdeclared in class X.

end example

]

[ Note

:

A static local variable or local type in a member function always refers to the same entity, whether or not the member function is inline.

end note

]

Previously declared member functions may be mentioned in friend declarations.

Member functions of a local class shall be defined inline in their class definition, if they are defined at all.

[ Note

:

A member function can be declared (but not defined) using a typedef for a function type.

The resulting member function has exactly the same type as it would have if the function declarator were provided explicitly, see [dcl.fct].

For example,

typedef void fv(); typedef void fvc() const; struct S { fv memfunc1;
void memfunc2(); fvc memfunc3;
}; fv S::* pmfv1 = &S::memfunc1; fv S::* pmfv2 = &S::memfunc2; fvc S::* pmfv3 = &S::memfunc3;

end note

]

11.4.2 Non-static member functions [class.mfct.non-static]

A non-static member function may be called for an object of its class type, or for an object of a class derivedfrom its class type, using theclass member access syntax ([over.match.call]).

A non-static member function may also be called directly using the function call syntax ([expr.call], [over.match.call]) from within its class or a class derived from its class, or a member thereof, as described below.

If a non-static member function of a class X is called for an object that is not of type X, or of a type derived fromX, the behavior is undefined.

[ Note

:

If C is not X or a base class of X, the class member access expression is ill-formed.

end note

]

This transformation does not apply in the template definition context ([temp.dep.type]).

[ Example

:

struct tnode { char tword[20]; int count; tnode* left; tnode* right; void set(const char*, tnode* l, tnode* r); };

void tnode::set(const char* w, tnode* l, tnode* r) { count = strlen(w)+1; if (sizeof(tword)<=count) perror("tnode string too long"); strcpy(tword,w); left = l; right = r; }

void f(tnode n1, tnode n2) { n1.set("abc",&n2,0); n2.set("def",0,0); }

In the body of the member function tnode​::​set, the member namestword, count, left, and right refer to members of the object for which the function is called.

Thus, in the call n1.set("abc",&n2,0), tword refers ton1.tword, and in the call n2.set("def",0,0), it refers to n2.tword.

The functions strlen, perror, andstrcpy are not members of the class tnode and should be declared elsewhere.102

end example

]

A non-static member function may be declared const,volatile, or const volatile.

They also affect the function type of the member function; a member function declaredconst is a const member function, a member function declared volatile is a volatile member function and a member function declared const volatile is aconst volatile member function.

[ Example

:

struct X { void g() const; void h() const volatile; };

X​::​g is a const member function and X​::​h is a const volatile member function.

end example

]

11.4.2.1 The this pointer [class.this]

In the body of a non-static ([class.mfct]) member function, the keyword this is a prvalue whose value is a pointer to the object for which the function is called.

The type of this in a member function whose type has a cv-qualifier-seq cv and whose class is Xis “pointer to cv X”.

[ Note

:

Thus in a const member function, the object for which the function is called is accessed through a const access path.

end note

]

[ Example

:

struct s { int a; int f() const; int g() { return a++; } int h() const { return a++; } };

int s::f() const { return a; }

The a++ in the body of s​::​h is ill-formed because it tries to modify (a part of) the object for which s​::​h() is called.

This is not allowed in a const member function becausethis is a pointer to const; that is, *this hasconst type.

end example

]

[ Note

:

Similarly, volatile semantics apply in volatile member functions when accessing the object and its non-static data members.

end note

]

[ Example

:

void k(s& x, const s& y) { x.f(); x.g(); y.f(); y.g();
}

The call y.g() is ill-formed because y is constand s​::​g() is a non-const member function, that is,s​::​g() is less-qualified than the object expression y.

end example

]

[ Note

:

Constructors and destructors cannot be declared const, volatile, or const volatile.

However, these functions can be invoked to create and destroy objects with cv-qualified types; see [class.ctor] and [class.dtor].

end note

]

11.4.3 Special member functions [special]

Default constructors ([class.default.ctor]), copy constructors, move constructors ([class.copy.ctor]), copy assignment operators, move assignment operators ([class.copy.assign]), and prospective destructors ([class.dtor]) arespecial member functions.

[ Note

:

The implementation will implicitly declare these member functions for some class types when the program does not explicitly declare them.

The implementation will implicitly define them if they are odr-used ([basic.def.odr]) or needed for constant evaluation ([expr.const]).

end note

]

An implicitly-declared special member function is declared at the closing} of the class-specifier.

Programs shall not define implicitly-declared special member functions.

Programs may explicitly refer to implicitly-declared special member functions.

[ Example

:

A program may explicitly call or form a pointer to member to an implicitly-declared special member function.

struct A { };
struct B : A { B& operator=(const B &); }; B& B::operator=(const B& s) { this->A::operator=(s);
return *this; }

end example

]

[ Note

:

The special member functions affect the way objects of class type are created, copied, moved, and destroyed, and how values can be converted to values of other types.

Often such special member functions are called implicitly.

end note

]

Special member functions obey the usual access rules ([class.access]).

[ Example

:

Declaring a constructor protected ensures that only derived classes and friends can create objects using it.

end example

]

Two special member functions are of the same kind if:

An eligible special member function is a special member function for which:

For a class, its non-static data members, its non-virtual direct base classes, and, if the class is not abstract ([class.abstract]), its virtual base classes are called its potentially constructed subobjects.

A defaulted special member function isconstexpr-compatibleif the corresponding implicitly-declared special member function would be a constexpr function.

11.4.4 Constructors [class.ctor]

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

]

An object created in this way is unnamed.

[ Note

:

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.

Areturnstatement in the body of a constructor shall not specify a return value.

The address of a constructor shall not be taken.

A constructor shall not be a coroutine.

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:

A default constructor istrivialif it is not user-provided and if:

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

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

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

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:

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.

11.4.5 Copy/move assignment operator [class.copy.assign]

A user-declared copy assignment operator X​::​operator= is a non-static non-template member function of class X with exactly one parameter of type X, X&, const X&,volatile X&, or const volatile X&.104

[ Note

:

An overloaded assignment operator must be declared to have only one parameter; see [over.ass].

end note

]

[ Note

:

More than one form of copy assignment operator may be declared for a class.

end note

]

[ Note

:

If a classXonly has a copy assignment operator with a parameter of typeX&, an expression of type constXcannot be assigned to an object of typeX.

[ Example

:

struct X { X(); X& operator=(X&); }; const X cx; X x; void f() { x = cx;
}

end example

]

end note

]

If the class definition does not explicitly declare a copy assignment operator, one is declared implicitly.

If the class definition declares a move constructor or move assignment operator, the implicitly declared copy assignment operator 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 constructor or a user-declared destructor ([depr.impldec]).

The implicitly-declared copy assignment operator for a classXwill have the form

X& X::operator=(const X&)

if

Otherwise, the implicitly-declared copy assignment operator will have the form

X& X::operator=(X&)

A user-declared move assignment operator X​::​operator= is a non-static non-template member function of class X with exactly one parameter of type X&&, const X&&, volatile X&&, orconst volatile X&&.

[ Note

:

An overloaded assignment operator must be declared to have only one parameter; see [over.ass].

end note

]

[ Note

:

More than one form of move assignment operator may be declared for a class.

end note

]

If the definition of a class X does not explicitly declare a move assignment operator, one will be implicitly declared as defaulted if and only if

[ Example

:

The class definition

struct S { int a; S& operator=(const S&) = default; };

will not have a default move assignment operator implicitly declared because the copy assignment operator has been user-declared.

The move assignment operator may be explicitly defaulted.

struct S { int a; S& operator=(const S&) = default; S& operator=(S&&) = default; };

end example

]

The implicitly-declared move assignment operator for a class X will have the form

X& X::operator=(X&&)

The implicitly-declared copy/move assignment operator for classXhas the return typeX&; it returns the object for which the assignment operator is invoked, that is, the object assigned to.

An implicitly-declared copy/move assignment operator is an inline public member of its class.

A defaulted copy/move assignment operator for class X is defined as deleted if X has:

[ Note

:

A defaulted move assignment operator that is defined as deleted is ignored by overload resolution ([over.match], [over.over]).

end note

]

Because a copy/move assignment operator is implicitly declared for a class if not declared by the user, a base class copy/move assignment operator is always hidden by the corresponding assignment operator of a derived class ([over.ass]).

Ausing-declaration that brings in from a base class an assignment operator with a parameter type that could be that of a copy/move assignment operator for the derived class is not considered an explicit declaration of such an operator and does not suppress the implicit declaration of the derived class operator; the operator introduced by theusing-declarationis hidden by the implicitly-declared operator in the derived class.

A copy/move assignment operator for classXis trivial if it is not user-provided and if:

otherwise the copy/move assignment operator isnon-trivial.

A copy/move assignment operator for a class Xthat is defaulted and not defined as deleted isimplicitly definedwhen it is odr-used ([basic.def.odr]) (e.g., when it is selected by overload resolution to assign to an object of its class type), when it is needed for constant evaluation ([expr.const]), or when it is explicitly defaulted after its first declaration.

The implicitly-defined copy/move assignment operator is constexpr if

Before the defaulted copy/move assignment operator for a class is implicitly defined, all non-user-provided copy/move assignment operators for its direct base classes and its non-static data members are implicitly defined.

[ Note

:

An implicitly-declared copy/move assignment operator has an implied exception specification ([except.spec]).

end note

]

The implicitly-defined copy/move assignment operator for a non-union class X performs memberwise copy/move assignment of its subobjects.

The direct base classes of X are assigned first, in the order of their declaration in thebase-specifier-list, and then the immediate non-static data members ofX are assigned, in the order in which they were declared in the class definition.

Let x be either the parameter of the function or, for the move operator, an xvalue referring to the parameter.

Each subobject is assigned in the manner appropriate to its type:

It is unspecified whether subobjects representing virtual base classes are assigned more than once by the implicitly-defined copy/move assignment operator.

[ Example

:

struct V { }; struct A : virtual V { }; struct B : virtual V { }; struct C : B, A { };

It is unspecified whether the virtual base class subobjectVis assigned twice by the implicitly-defined copy/move assignment operator forC.

end example

]

The implicitly-defined copy assignment operator for a union X copies the object representation ([basic.types]) of X.

If the source and destination of the assignment are not the same object, then 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 created, and the lifetime of o begins before the copy is performed.

11.4.6 Destructors [class.dtor]

A prospective destructor shall take no arguments ([dcl.fct]).

Each decl-specifier of the decl-specifier-seqof a prospective destructor declaration (if any) shall befriend,inline,virtual,constexpr, orconsteval.

If a class has no user-declared prospective destructor, a prospective destructor is implicitly declared as defaulted ([dcl.fct.def]).

An implicitly-declared prospective destructor is an inline public member of its class.

An implicitly-declared prospective destructor for a class X will have the form

~X()

At the end of the definition of a class, overload resolution is performed among the prospective destructors declared in that class with an empty argument list to select the destructor for the class, also known as the selected destructor.

The program is ill-formed if overload resolution fails.

Destructor selection does not constitute a reference to, or odr-use ([basic.def.odr]) of, the selected destructor, and in particular, the selected destructor may be deleted ([dcl.fct.def.delete]).

The address of a destructor shall not be taken.

A destructor can be invoked for aconst,volatileorconst volatileobject.

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

They stop being in effect when the destructor for the most derived object ([intro.object]) starts.

[ Note

:

A declaration of a destructor that does not have a noexcept-specifierhas the same exception specification as if it had been implicitly declared ([except.spec]).

end note

]

A defaulted destructor for a classX is defined as deleted if:

A destructor is trivial if it is not user-provided and if:

Otherwise, the destructor isnon-trivial.

A defaulted destructor is a constexpr destructor if it satisfies the requirements for a constexpr destructor ([dcl.constexpr]).

A destructor that is defaulted and not defined as deleted isimplicitly definedwhen it is odr-used ([basic.def.odr]) or when it is explicitly defaulted after its first declaration.

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

If the destructor of a class is virtual and any objects of that class or any derived class are created in the program, the destructor shall be defined.

If a class has a base class with a virtual destructor, its destructor (whether user- or implicitly-declared) is virtual.

[ Note

:

Some language constructs have special semantics when used during destruction; see [class.cdtor].

end note

]

After executing the body of the destructor and destroying any objects with automatic storage duration allocated within the body, a destructor for classXcalls the destructors forX's direct non-variant non-static data members, the destructors forX's non-virtual direct base classes and, ifXis the most derived class ([class.base.init]), its destructor calls the destructors forX's virtual base classes.

All destructors are called as if they were referenced with a qualified name, that is, ignoring any possible virtual overriding destructors in more derived classes.

Bases and members are destroyed in the reverse order of the completion of their constructor (see [class.base.init]).

Areturnstatement ([stmt.return]) in a destructor might not directly return to the caller; before transferring control to the caller, the destructors for the members and bases are called.

Destructors for elements of an array are called in reverse order of their construction (see [class.init]).

A destructor is invoked implicitly

In each case, the context of the invocation is the context of the construction of the object.

[ Note

:

An array of class type contains several subobjects for each of which the destructor is invoked.

end note

]

A destructor can also be invoked explicitly.

A program is ill-formed if a destructor that is potentially invoked is deleted or not accessible from the context of the invocation.

At the point of definition of a virtual destructor (including an implicit definition ([class.dtor])), the non-array deallocation function is determined as if for the expression delete this appearing in a non-virtual destructor of the destructor's class (see [expr.delete]).

If the lookup fails or if the deallocation function has a deleted definition ([dcl.fct.def]), the program is ill-formed.

[ Note

:

This assures that a deallocation function corresponding to the dynamic type of an object is available for thedelete-expression ([class.free]).

end note

]

In an explicit destructor call, the destructor is specified by a~followed by atype-name or decltype-specifierthat denotes the destructor's class type.

The invocation of a destructor is subject to the usual rules for member functions ([class.mfct]); that is, if the object is not of the destructor's class type and not of a class derived from the destructor's class type (including when the destructor is invoked via a null pointer value), the program has undefined behavior.

[ Note

:

Invoking delete on a null pointer does not call the destructor; see [expr.delete].

end note

]

[ Example

:

struct B { virtual ~B() { } }; struct D : B { ~D() { } };

D D_object; typedef B B_alias; B* B_ptr = &D_object;

void f() { D_object.B::B();
B_ptr->
B();
B_ptr->B_alias();
B_ptr->B_alias::
B();
B_ptr->B_alias::~B_alias();
}

end example

]

[ Note

:

Explicit calls of destructors are rarely needed.

One use of such calls is for objects placed at specific addresses using a placementnew-expression.

Such use of explicit placement and destruction of objects can be necessary to cope with dedicated hardware resources and for writing memory management facilities.

For example,

void* operator new(std::size_t, void* p) { return p; } struct X { X(int); ~X(); }; void f(X* p);

void g() {
char* buf = new char[sizeof(X)]; X* p = new(buf) X(222);
f(p); p->X::~X();
}

end note

]

Once a destructor is invoked for an object, the object no longer exists; the behavior is undefined if the destructor is invoked for an object whose lifetime has ended ([basic.life]).

[ Example

:

If the destructor for an object with automatic storage duration is explicitly invoked, and the block is subsequently left in a manner that would ordinarily invoke implicit destruction of the object, the behavior is undefined.

end example

]

[ Note

:

The notation for explicit call of a destructor can be used for any scalar type name ([expr.prim.id.dtor]).

Allowing this makes it possible to write code without having to know if a destructor exists for a given type.

For example:

typedef int I; I* p; p->I::~I();

end note

]

A destructor shall not be a coroutine.

11.4.7 Conversions [class.conv]

Type conversions of class objects can be specified by constructors and by conversion functions.

[ Note

:

See [over.match] for a discussion of the use of conversions in function calls as well as examples below.

end note

]

At most one user-defined conversion (constructor or conversion function) is implicitly applied to a single value.

[ Example

:

struct X { operator int(); };

struct Y { operator X(); };

Y a; int b = a;
int c = X(a);

end example

]

User-defined conversions are used implicitly only if they are unambiguous.

A conversion function in a derived class does not hide a conversion function in a base class unless the two functions convert to the same type.

Function overload resolution ([over.match.best]) selects the best conversion function to perform the conversion.

[ Example

:

struct X { operator int(); };

struct Y : X { operator char(); };

void f(Y& a) { if (a) {
} }

end example

]

11.4.7.1 Conversion by constructor [class.conv.ctor]

A constructor that is not explicit ([dcl.fct.spec]) specifies a conversion from the types of its parameters (if any) to the type of its class.

Such a constructor is called aconverting constructor.

[ Example

:

struct X { X(int); X(const char*, int =0); X(int, int); };

void f(X arg) { X a = 1;
X b = "Jessie";
a = 2;
f(3);
f({1, 2});
}

end example

]

[ Note

:

An explicit constructor constructs objects just like non-explicit constructors, but does so only where the direct-initialization syntax ([dcl.init]) or where casts ([expr.static.cast], [expr.cast]) are explicitly used; see also [over.match.copy].

A default constructor may be an explicit constructor; such a constructor will be used to perform default-initialization or value-initialization ([dcl.init]).

[ Example

:

struct Z { explicit Z(); explicit Z(int); explicit Z(int, int); };

Z a;
Z b{};
Z c = {};
Z a1 = 1;
Z a3 = Z(1);
Z a2(1);
Z* p = new Z(1);
Z a4 = (Z)1;
Z a5 = static_cast(1);
Z a6 = { 3, 4 };

end example

]

end note

]

A non-explicit copy/move constructor ([class.copy.ctor]) is a converting constructor.

[ Note

:

An implicitly-declared copy/move constructor is not an explicit constructor; it may be called for implicit type conversions.

end note

]

11.4.7.2 Conversion functions [class.conv.fct]

A member function of a class X having no parameters with a name of the form

conversion-function-id: operator conversion-type-id

conversion-type-id: type-specifier-seq conversion-declarator

Such functions are called conversion functions.

The type of the conversion function ([dcl.fct]) is “function taking no parameter returningconversion-type-id.

A conversion function is never used to convert a (possibly cv-qualified) object to the (possibly cv-qualified) same object type (or a reference to it), to a (possibly cv-qualified) base class of that type (or a reference to it), or to cv void.106

[ Example

:

struct X { operator int(); operator auto() -> short;
};

void f(X a) { int i = int(a); i = (int)a; i = a; }

In all three cases the value assigned will be converted byX​::​operator int().

end example

]

A conversion function may be explicit ([dcl.fct.spec]), in which case it is only considered as a user-defined conversion for direct-initialization ([dcl.init]).

Otherwise, user-defined conversions are not restricted to use in assignments and initializations.

[ Example

:

class Y { }; struct Z { explicit operator Y() const; };

void h(Z z) { Y y1(z);
Y y2 = z;
Y y3 = (Y)z;
}

void g(X a, X b) { int i = (a) ? 1+a : 0; int j = (a&&b) ? a+b : i; if (a) { } }

end example

]

Theconversion-type-idshall not represent a function type nor an array type.

Theconversion-type-idin aconversion-function-idis the longest sequence of tokens that could possibly form a conversion-type-id.

[ Note

:

This prevents ambiguities between the declarator operator * and its expression counterparts.

[ Example

:

&ac.operator int*i;

The * is the pointer declarator and not the multiplication operator.

end example

]

This rule also prevents ambiguities for attributes.

[ Example

:

operator int [[noreturn]] ();

end example

]

end note

]

Conversion functions are inherited.

Conversion functions can be virtual.

A conversion function template shall not have a deduced return type ([dcl.spec.auto]).

[ Example

:

struct S { operator auto() const { return 10; }
template operator auto() const { return 1.2; }
};

end example

]

11.4.8 Static members [class.static]

A static member s of class X may be referred to using the qualified-id expression X​::​s; it is not necessary to use the class member access syntax ([expr.ref]) to refer to a static member.

A static member may be referred to using the class member access syntax, in which case the object expression is evaluated.

[ Example

:

struct process { static void reschedule(); }; process& g();

void f() { process::reschedule();
g().reschedule();
}

end example

]

A static member may be referred to directly in the scope of its class or in the scope of a class derived ([class.derived]) from its class; in this case, the static member is referred to as if a qualified-id expression was used, with thenested-name-specifier of the qualified-id naming the class scope from which the static member is referenced.

[ Example

:

int g(); struct X { static int g(); }; struct Y : X { static int i; }; int Y::i = g();

end example

]

Static members obey the usual class member access rules ([class.access]).

When used in the declaration of a class member, the static specifier shall only be used in the member declarations that appear within the member-specification of the class definition.

[ Note

:

It cannot be specified in member declarations that appear in namespace scope.

end note

]

11.4.8.1 Static member functions [class.static.mfct]

[ Note

:

The rules described in [class.mfct] apply to static member functions.

end note

]

[ Note

:

A static member function does not have a thispointer ([class.this]).

end note

]

A static member function shall not be virtual.

There shall not be a static and a non-static member function with the same name and the same parameter types ([over.load]).

A static member function shall not be declared const,volatile, or const volatile.

11.4.8.2 Static data members [class.static.data]

A static data member is not part of the subobjects of a class.

If a static data member is declared thread_­local there is one copy of the member per thread.

If a static data member is not declaredthread_­local there is one copy of the data member that is shared by all the objects of the class.

A static data member shall not be mutable ([dcl.stc]).

A static data member shall not be a direct member ([class.mem]) of an unnamed ([class.pre]) or local ([class.local]) class or of a (possibly indirectly) nested class ([class.nest]) thereof.

The declaration of a non-inline static data member in its class definition is not a definition and may be of an incomplete type other thancv void.

The definition for a static data member that is not defined inline in the class definition shall appear in a namespace scope enclosing the member's class definition.

In the definition at namespace scope, the name of the static data member shall be qualified by its class name using the ​::​operator.

[ Example

:

class process { static process* run_chain; static process* running; };

process* process::running = get_main(); process* process::run_chain = running;

The static data member run_­chain of classprocess is defined in global scope; the notationprocess​::​run_­chain specifies that the member run_­chainis a member of class process and in the scope of classprocess.

In the static data member definition, theinitializer expression refers to the static data member running of class process.

end example

]

[ Note

:

Once the static data member has been defined, it exists even if no objects of its class have been created.

[ Example

:

In the example above, run_­chain and running exist even if no objects of class process are created by the program.

end example

]

end note

]

The member shall still be defined in a namespace scope if it is odr-used ([basic.def.odr]) in the program and the namespace scope definition shall not contain an initializer.

An inline static data member may be defined in the class definition and may specify a brace-or-equal-initializer.

If the member is declared with the constexpr specifier, it may be redeclared in namespace scope with no initializer (this usage is deprecated; see [depr.static.constexpr]).

[ Note

:

There is exactly one definition of a static data member that is odr-used ([basic.def.odr]) in a valid program.

end note

]

[ Note

:

Static data members of a class in namespace scope have the linkage of the name of the class ([basic.link]).

end note

]

11.4.9 Bit-fields [class.bit]

A bit-field shall not be a static member.

A bit-field shall have integral or enumeration type; the bit-field semantic property is not part of the type of the class member.

The constant-expression shall be an integral constant expression with a value greater than or equal to zero and is called the width of the bit-field.

If the width of a bit-field is larger than the width of the bit-field's type (or, in case of an enumeration type, of its underlying type), the extra bits are padding bits ([basic.types]).

Allocation of bit-fields within a class object isimplementation-defined.

Alignment of bit-fields is implementation-defined.

Bit-fields are packed into some addressable allocation unit.

[ Note

:

Bit-fields straddle allocation units on some machines and not on others.

Bit-fields are assigned right-to-left on some machines, left-to-right on others.

end note

]

A declaration for a bit-field that omits the identifierdeclares an unnamed bit-field.

Unnamed bit-fields are not members and cannot be initialized.

An unnamed bit-field shall not be declared with a cv-qualified type.

[ Note

:

An unnamed bit-field is useful for padding to conform to externally-imposed layouts.

end note

]

As a special case, an unnamed bit-field with a width of zero specifies alignment of the next bit-field at an allocation unit boundary.

Only when declaring an unnamed bit-field may the width be zero.

The address-of operator & shall not be applied to a bit-field, so there are no pointers to bit-fields.

A non-const reference shall not be bound to a bit-field ([dcl.init.ref]).

[ Note

:

If the initializer for a reference of type const T& is an lvalue that refers to a bit-field, the reference is bound to a temporary initialized to hold the value of the bit-field; the reference is not bound to the bit-field directly.

end note

]

If a value of integral type (other than bool) is stored into a bit-field of width N and the value would be representable in a hypothetical signed or unsigned integer type with width N and the same signedness as the bit-field's type, the original value and the value of the bit-field compare equal.

If the value true or false is stored into a bit-field of type bool of any size (including a one bit bit-field), the original bool value and the value of the bit-field compare equal.

If a value of an enumeration type is stored into a bit-field of the same type and the width is large enough to hold all the values of that enumeration type ([dcl.enum]), the original value and the value of the bit-field compare equal.

[ Example

:

enum BOOL { FALSE=0, TRUE=1 }; struct A { BOOL b:1; }; A a; void f() { a.b = TRUE; if (a.b == TRUE)
{ } }

end example

]

11.4.10 Nested class declarations [class.nest]

A class can be declared within another class.

A class declared within another is called a nested class.

The name of a nested class is local to its enclosing class.

The nested class is in the scope of its enclosing class.

[ Note

:

See [expr.prim.id] for restrictions on the use of non-static data members and non-static member functions.

end note

]

[ Example

:

int x; int y;

struct enclose { int x; static int s;

struct inner { void f(int i) { int a = sizeof(x);
x = i;
s = i;
::x = i;
y = i;
} void g(enclose* p, int i) { p->x = i;
} }; };

inner* p = 0;

end example

]

Member functions and static data members of a nested class can be defined in a namespace scope enclosing the definition of their class.

[ Example

:

struct enclose { struct inner { static int x; void f(int i); }; };

int enclose::inner::x = 1;

void enclose::inner::f(int i) { }

end example

]

If class X is defined in a namespace scope, a nested classY may be declared in class X and later defined in the definition of class X or be later defined in a namespace scope enclosing the definition of class X.

[ Example

:

class E { class I1;
class I2; class I1 { };
}; class E::I2 { };

end example

]

Like a member function, a friend function ([class.friend]) defined within a nested class is in the lexical scope of that class; it obeys the same rules for name binding as a static member function of that class ([class.static]), but it has no special access rights to members of an enclosing class.

11.4.11 Nested type names [class.nested.type]

Type names obey exactly the same scope rules as other names.

In particular, type names defined within a class definition cannot be used outside their class without qualification.

[ Example

:

struct X { typedef int I; class Y { }; I a; };

I b;
Y c;
X::Y d;
X::I e;

end example

]