[class.mem] (original) (raw)
11 Classes [class]
11.4 Class members [class.mem]
11.4.1 General [class.mem.general]
virt-specifier:
override
final
[Note 1:
If the member declaration acquires a function type through template instantiation, the program is ill-formed; see [temp.spec.general].
— _end note_]
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 members ([class.union.anon]) and direct members thereof.
Members of a class are data members, member functions ([class.mfct]), nested types, enumerators, and member templates ([temp.mem]) and specializations thereof.
[Note 2:
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 3:
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
- a nested class or member class template can be declared and then later defined, and
- an enumeration can be introduced with an opaque-enum-declaration and later redeclared with an enum-specifier.
[Note 4:
A single name can denote several member functions provided their types are sufficiently different ([basic.scope.scope]).
— _end note_]
A redeclaration of a class member outside its class definition shall be a definition, an explicit specialization, or an explicit instantiation ([temp.expl.spec], [temp.explicit]).
The member shall not be a non-static data member.
[Note 5:
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 C is complete at a program point Pif the definition of C is reachable from P ([module.reach]) or if P is in a complete-class context of C.
Otherwise, C is incomplete at P.
If a member-declaration matches the syntactic requirements of friend-type-declaration, it is a friend-type-declaration.
[Example 1: struct S { using T = void(); T * p = 0; virtual T f = 0; }; — _end example_]
[Example 2: 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.
An immediate invocation ([expr.const]) that is a potentially-evaluated subexpression ([intro.execution]) of a default member initializer is neither evaluated nor checked for whether it is a constant expression at the point where the subexpression appears.
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.general]), an abstract class type ([class.abstract]), or a (possibly multidimensional) array thereof.
[Note 6:
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 7:
See [expr.prim.id] for restrictions on the use of non-static data members and non-static member functions.
— _end note_]
[Note 8:
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 3:
A simple example of a class definition isstruct 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 declarationtnode 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 9:
Non-variant non-static data members of non-zero size ([intro.object]) are allocated so that later members have higher addresses within a class object ([expr.rel]).
Implementation alignment requirements can cause two adjacent members not to be allocated immediately after each other; so can 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:
- every static data member of class T;
- every member function of class T;
[Note 10:
This restriction does not apply to constructors, which do not have names ([class.ctor]).
— _end note_] - every member of class T that is itself a type;
- every member template of class T;
- every enumerator of every member of class T that is an unscoped enumeration type; and
- every member of every anonymous union that is a member of classT.
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 ([basic.types]),
- corresponding entities have the same alignment requirements ([basic.align]),
- if a has-attribute-expression ([cpp.cond]) is not 0 for the no_unique_address attribute, then neither entity is declared with the no_unique_address attribute ([dcl.attr.nouniqueaddr]), and
- either both entities are bit-fields with the same width or neither is a bit-field.
[Example 4: 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 ([basic.types.general]).
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 5: 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 11:
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 12:
There can 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.2 Member functions [class.mfct]
If a member function is attached to the global module and is defined ([dcl.fct.def]) in its class definition, it is inline ([dcl.inline]).
[Note 1:
A member function is also inline if it is declaredinline, constexpr, or consteval.
— _end note_]
[Example 1: struct X { typedef int T;static T count;void f(T);};void X::f(T t = count) { }
The definition of the member function f of class X inhabits the global scope; the notation X::f indicates 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_]
Member functions of a local class shall be defined inline in their class definition, if they are defined at all.
[Note 2:
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] and [temp.arg].
[Example 2: 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 example_]
— _end note_]
11.4.3 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 derived ([class.derived]) from its class type, using the class member access syntax ([expr.ref], [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.
11.4.4 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 1:
The implementation will implicitly declare these member functions for some class types when the program does not explicitly declare them.
— _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 1:
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 2:
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 2:
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
- they are both default constructors,
- they are both copy or move constructors with the same first parameter type, or
- they are both copy or move assignment operators with the same first parameter type and the same cv-qualifiers and ref-qualifier, if any.
An eligible special member function is a special member function for which:
- the function is not deleted,
- the associated constraints ([temp.constr]), if any, are satisfied, and
- no special member function of the same kind whose associated constraints, if any, are satisfied is more constrained ([temp.constr.order]).
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.
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
- any non-static data member with no default member initializer ([class.mem]) is of reference type,
- X is a non-union class and any non-variant non-static data member of const-qualified type (or possibly multidimensional array thereof) with no brace-or-equal-initializeris not const-default-constructible ([dcl.init]),
- any non-variant potentially constructed subobject, except for a non-static data member with a brace-or-equal-initializer, has class type M (or possibly multidimensional array thereof) and overload resolution ([over.match]) as applied to find M's corresponding constructor does not result in a usable candidate ([over.match.general]), or
- any potentially constructed subobject S has class type M (or possibly multidimensional array thereof),M has a destructor that is deleted or inaccessible from the defaulted default constructor, and either S is non-variant or S has a default member initializer.
A default constructor for a class X istrivialif it is not user-provided and if
- X has no virtual functions ([class.virtual]) and no virtual base classes ([class.mi]), and
- no non-static data member of X has a default member initializer ([class.mem]), and
- all the direct base classes of X have trivial default constructors, and
- either X is a union or for all the non-variant non-static data members of X that are of class type (or array thereof), each such class has a trivial default constructor.
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
- 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 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:
- a potentially constructed subobject of typeM (or possibly multidimensional array thereof) for which overload resolution ([over.match]), as applied to findM's corresponding constructor, either does not result in a usable candidate ([over.match.general]) or, in the case of a variant member, selects a non-trivial function,
- any potentially constructed subobject of class type M (or possibly multidimensional array thereof) where M has 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 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
- 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.
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:
- 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.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.
11.4.6 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 non-object parameter of type X, X&, const X&,volatile X&, or const volatile X&.90
[Note 1:
More than one form of copy assignment operator can be declared for a class.
— _end note_]
[Note 2:
If a classXonly has a copy assignment operator with a non-object parameter of typeX&, an expression of type constXcannot be assigned to an object of typeX.
[Example 1: 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 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 formX& X::operator=(const X&) if
- each direct base class B of Xhas a copy assignment operator whose non-object parameter is of typeconst B&, const volatile B&, or B, and
- for all the non-static data members of Xthat are of a class type M (or array thereof), each such class type has a copy assignment operator whose non-object parameter is of typeconst M&, const volatile M&, or M.91
Otherwise, the implicitly-declared copy assignment operator will have the formX& 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 non-object parameter of type X&&, const X&&, volatile X&&, orconst volatile X&&.
[Note 3:
More than one form of move assignment operator can 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
- X does not have a user-declared copy constructor,
- X does not have a user-declared move constructor,
- X does not have a user-declared copy assignment operator, and
- X does not have a user-declared destructor.
[Example 2:
The class definitionstruct 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 formX& X::operator=(X&&)
The implicitly-declared copy/move assignment operator for classXhas the return typeX&.
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:
- a non-static data member of const non-class type (or possibly multidimensional array thereof), or
- a non-static data member of reference type, or
- a direct non-static data member of class type M (or possibly multidimensional array thereof) or a direct base class M that cannot be copied/moved because overload resolution ([over.match]), as applied to find M's corresponding assignment operator, either does not result in a usable candidate ([over.match.general]) or, in the case of a variant member, selects a non-trivial function.
[Note 4:
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.assign]).
[Note 5:
A using-declaration in a derived class Cthat names an assignment operator from a base class never suppresses the implicit declaration of an assignment operator of C, even if the base class assignment operator would be a copy or move assignment operator if declared as a member of C.
— _end note_]
A copy/move assignment operator 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 assignment operator 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 assignment operator selected to copy/move that member is trivial;
otherwise the copy/move assignment operator isnon-trivial.
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 6:
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:
- if the subobject is of class type, as if by a call to operator= with the subobject as the object expression and the corresponding subobject of x as a single function argument (as if by explicit qualification; that is, ignoring any possible virtual overriding functions in more derived classes);
- if the subobject is an array, each element is assigned, in the manner appropriate to the element type;
- if the subobject is of scalar type, the built-in assignment operator is used.
It is unspecified whether subobjects representing virtual base classes are assigned more than once by the implicitly-defined copy/move assignment operator.
[Example 3: 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/move assignment operator for a union X copies the object representation ([basic.types.general]) 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.
The implicitly-defined copy/move assignment operator for a class returns the object for which the assignment operator is invoked, that is, the object assigned to.
11.4.7 Destructors [class.dtor]
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.
[Note 1:
A return statement in the body of a destructor cannot specify a return value ([stmt.return]).
— _end note_]
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 2:
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
- X is a non-union class and any non-variant potentially constructed subobject has class typeM (or possibly multidimensional array thereof) whereM has a destructor that is deleted or is inaccessible from the defaulted destructor,
- X is a union and
- overload resolution to select a constructor to default-initialize an object of type X either fails or selects a constructor that is either deleted or not trivial, or
- X has a variant member V of class type M (or possibly multi-dimensional array thereof) where V has a default member initializer andM has a destructor that is non-trivial, or,
- for a virtual destructor, lookup of the non-array deallocation function results in an ambiguity or in a function that is deleted or inaccessible from the defaulted destructor.
A destructor for a class X is trivial if it is not user-provided and if
- the destructor is not virtual,
- all of the direct base classes of X have trivial destructors, and
- either X is a union or for all of the non-variant non-static data members of X that are of class type (or array thereof), each such class has a trivial destructor.
Otherwise, the destructor isnon-trivial.
A defaulted destructor is a constexpr destructor if it is constexpr-suitable ([dcl.constexpr]).
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.
[Note 3:
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 other than anonymous unions, 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]).
[Note 4:
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.
— _end note_]
Destructors for elements of an array are called in reverse order of their construction (see [class.init]).
A destructor is invoked implicitly
- for a constructed object with static storage duration ([basic.stc.static]) at program termination ([basic.start.term]),
- for a constructed object with thread storage duration ([basic.stc.thread]) at thread exit,
- for a constructed object with automatic storage duration ([basic.stc.auto]) when the block in which an object is created exits ([stmt.dcl]),
- for a constructed temporary object when its lifetime ends ([conv.rval], [class.temporary]).
In each case, the context of the invocation is the context of the construction of the object.
[Note 5:
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), 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 6:
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 computed-type-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 7:
Invoking delete on a null pointer does not call the destructor; see [expr.delete].
— _end note_]
[Example 1: 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 9:
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.
[Example 2: 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 example_]
— _end note_]
Once a destructor is invoked for an object, the object's lifetime ends; the behavior is undefined if the destructor is invoked for an object whose lifetime has ended ([basic.life]).
[Example 3:
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 10:
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.8 Conversions [class.conv]
11.4.8.1 General [class.conv.general]
Type conversions of class objects can be specified by constructors and by conversion functions.
[Note 1:
See [over.match] for a discussion of the use of conversions in function calls.
— _end note_]
At most one user-defined conversion (constructor or conversion function) is implicitly applied to a single value.
[Example 1: struct X { operator int();};struct Y { operator X();}; Y a;int b = a; int c = X(a); — _end example_]
11.4.8.2 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.
[Example 1: 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 1:
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 can be an explicit constructor; such a constructor will be used to perform default-initialization or value-initialization ([dcl.init]).
[Example 2: 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<Z>(1); Z a6 = { 3, 4 }; — _end example_]
— _end note_]
11.4.8.3 Conversion functions [class.conv.fct]
A conversion function shall have no non-object parameters and shall be a non-static member function of a class or class template X; its declared return type is the conversion-type-id and it specifies a conversion from X to the type specified by the conversion-type-id, interpreted as a type-id ([dcl.name]).
[Note 1:
A conversion function is never invoked for implicit or explicit conversions of an object to the same object type (or a reference to it), to a base class of that type (or a reference to it), or to cv void.
Even though never directly called to perform a conversion, such conversion functions can be declared and can potentially be reached through a call to a virtual conversion function in a base class.
— _end note_]
[Example 1: 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 2: 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 2:
This prevents ambiguities between the declarator operator * and its expression counterparts.
[Example 3: &ac.operator int*i;
The * is the pointer declarator and not the multiplication operator.
— _end example_]
This rule also prevents ambiguities for attributes.
[Example 4: operator int [[noreturn]] (); — _end example_]
— _end note_]
[Note 3:
A conversion function in a derived class hides only conversion functions in base classes that convert to the same type.
A conversion function template with a dependent return type hides only templates in base classes that correspond to it ([class.member.lookup]); otherwise, it hides and is hidden as a non-template function.
Function overload resolution ([over.match.best]) selects the best conversion function to perform the conversion.
[Example 5: struct X { operator int();};struct Y : X { operator char();};void f(Y& a) { if (a) { } } — _end example_]
— _end note_]
Conversion functions can be virtual.
A conversion function template shall not have a deduced return type ([dcl.spec.auto]).
[Example 6: struct S { operator auto() const { return 10; } template<class T> operator auto() const { return 1.2; } }; — _end example_]
11.4.9 Static members [class.static]
11.4.9.1 General [class.static.general]
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 1: struct process { static void reschedule();}; process& g();void f() { process::reschedule(); g().reschedule(); } — _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 1:
It cannot be specified in member declarations that appear in namespace scope.
— _end note_]
11.4.9.2 Static member functions [class.static.mfct]
[Note 1:
The rules described in [class.mfct] apply to static member functions.
— _end note_]
[Note 2:
A static member function does not have a thispointer ([expr.prim.this]).
A static member function cannot be qualified with const,volatile, or virtual ([dcl.fct]).
— _end note_]
11.4.9.3 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.
[Example 1: class process { static process* run_chain;static process* running;}; process* process::running = get_main(); process* process::run_chain = running;
The definition of the static data member run_chain of classprocess inhabits the global scope; the notationprocess::run_chain indicates 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 2:
Once the static data member has been defined, it exists even if no objects of its class have been created.
[Example 2:
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.
The declaration of an inline static data member (which is a definition) 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 3:
There is exactly one definition of a static data member that is odr-used ([basic.def.odr]) in a valid program.
— _end note_]
[Note 4:
Static data members of a class in namespace scope have the linkage of the name of the class ([basic.link]).
— _end note_]
11.4.10 Bit-fields [class.bit]
A member-declarator of the form
specifies a bit-field.
A bit-field shall not be a static member.
A bit-field shall have integral or (possibly cv-qualified) 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.general]).
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 1:
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 2:
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 bind to a bit-field ([dcl.init.ref]).
[Note 3:
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 1: 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.11 Allocation and deallocation functions [class.free]
Any allocation function for a classTis a static member (even if not explicitly declaredstatic).
[Example 1: class Arena;struct B { void* operator new(std::size_t, Arena*);};struct D1 : B { }; Arena* ap;void foo(int i) { new (ap) D1; new D1[i]; new D1; } — _end example_]
Any deallocation function for a classXis a static member (even if not explicitly declaredstatic).
[Example 2: class X { void operator delete(void*);void operator delete[](void*, std::size_t);};class Y { void operator delete(void*, std::size_t);void operator delete[](void*);}; — _end example_]
Since member allocation and deallocation functions arestaticthey cannot be virtual.
[Note 1:
However, when thecast-expressionof adelete-expressionrefers to an object of class type with a virtual destructor, because the deallocation function is chosen by the destructor of the dynamic type of the object, the effect is the same in that case.
[Example 3: struct B { virtual ~B();void operator delete(void*, std::size_t);};struct D : B { void operator delete(void*);};struct E : B { void log_deletion();void operator delete(E *p, std::destroying_delete_t) { p->log_deletion(); p->~E();::operator delete(p);} };void f() { B* bp = new D;delete bp; bp = new E;delete bp; }
Here, storage for the object of classDis deallocated byD::operator delete(), and the object of class E is destroyed and its storage is deallocated by E::operator delete(), due to the virtual destructor.
— _end example_]
— _end note_]
[Note 2:
Virtual destructors have no effect on the deallocation function actually called when thecast-expressionof adelete-expressionrefers to an array of objects of class type.
[Example 4: struct B { virtual ~B();void operator delete[](void*, std::size_t);};struct D : B { void operator delete[](void*, std::size_t);};void f(int i) { D* dp = new D[i];delete [] dp; B* bp = new D[i];delete[] bp; } — _end example_]
— _end note_]
Access to the deallocation function is checked statically, even if a different one is actually executed.
[Example 5:
For the call on line “// 1” above, ifB::operator delete()had been private, the delete expression would have been ill-formed.
— _end example_]
11.4.12 Nested class declarations [class.nest]
A class can be declared within another class.
A class declared within another is called a nested class.
[Note 1:
See [expr.prim.id] for restrictions on the use of non-static data members and non-static member functions.
— _end note_]
[Example 1: 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_]
[Note 2:
Nested classes can be defined either in the enclosing class or in an enclosing namespace; member functions and static data members of a nested class can be defined either in the nested class or in an enclosing namespace scope.
[Example 2: struct enclose { struct inner { static int x;void f(int i);};};int enclose::inner::x = 1;void enclose::inner::f(int i) { } class E { class I1; class I2;class I1 { }; };class E::I2 { }; — _end example_]
— _end note_]
A friend function ([class.friend]) defined within a nested class has no special access rights to members of an enclosing class.