[class.mem.general] (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.

[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

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

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

[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_]