Files
cppdraft_translate/cppdraft/class/pre.md
2025-10-25 03:02:53 +03:00

10 KiB

[class.pre]

11 Classes [class]

11.1 Preamble [class.pre]

1

#

A class is a type.

Its name becomes a class-name ([class.name]) within its scope.

class-name:
identifier
simple-template-id

A class-specifier or an elaborated-type-specifier ([dcl.type.elab]) is used to make a class-name.

An object of a class consists of a (possibly empty) sequence of members and base class objects.

class-specifier:
class-head { member-specificationopt }

class-head:
class-key attribute-specifier-seqopt class-head-name class-property-specifier-seqopt base-clauseopt
class-key attribute-specifier-seqopt base-clauseopt

class-head-name:
nested-name-specifieropt class-name

class-property-specifier-seq:
class-property-specifier class-property-specifier-seqopt

class-property-specifier:
final
trivially_relocatable_if_eligible
replaceable_if_eligible

class-key:
class
struct
union

A class declaration where the class-name in the class-head-name is a simple-template-id shall be an explicit specialization ([temp.expl.spec]) or a partial specialization ([temp.spec.partial]).

A class-specifier whoseclass-head omits theclass-head-name defines an unnamed class.

[Note 1:

An unnamed class thus can't be final.

— end note]

Otherwise, the class-name is an identifier; it is not looked up, and the class-specifier introduces it.

2

#

The component name of theclass-name is also bound in the scope of the class (template) itself; this is known as the injected-class-name.

For purposes of access checking, the injected-class-name is treated as if it were a public member name.

A class-specifier is commonly referred to as a class definition.

A class is considered defined after the closing brace of itsclass-specifier has been seen even though its member functions are in general not yet defined.

The optional attribute-specifier-seq appertains to the class; the attributes in the attribute-specifier-seq are thereafter considered attributes of the class whenever it is named.

3

#

If a class-head-name contains a nested-name-specifier, the class-specifier shall not inhabit a class scope.

If its class-name is an identifier, the class-specifier shall correspond to one or more declarations nominable in the class, class template, or namespace to which thenested-name-specifier refers; they shall all have the same target scope, and the target scope of theclass-specifier is that scope.

[Example 1: namespace N {templatestruct A {struct B; };}using N::A;template struct A::B {}; // OKtemplate<> struct A {}; // OK — end example]

4

#

[Note 2:

The class-key determines whether the class is a union ([class.union]) and whether access is public or private by default ([class.access]).

A union holds the value of at most one data member at a time.

— end note]

5

#

Each class-property-specifier shall appear at most once within a single class-property-specifier-seq.

Whenever a class-key is followed by a class-head-name, one of the identifiers final, trivially_relocatable_if_eligible, or replaceable_if_eligible, and a colon or left brace, the identifier is interpreted as a class-property-specifier.

[Example 2: struct A;struct A final {}; // OK, definition of struct A,// not value-initialization of variable finalstruct X {struct C { constexpr operator int() { return 5; } }; struct B trivially_relocatable_if_eligible : C{}; // OK, definition of nested class B,// not declaration of a bit-field member// trivially_relocatable_if_eligible}; — end example]

6

#

If a class is marked with the class-property-specifierfinal and that class appears as a class-or-decltype in a base-clause ([class.derived]), the program is ill-formed.

7

#

[Note 3:

Complete objects of class type have nonzero size.

Base class subobjects and members declared with the no_unique_address attribute ([dcl.attr.nouniqueaddr]) are not so constrained.

— end note]

8

#

[Note 4:

Class objects can be assigned ([over.assign], [class.copy.assign]), passed as arguments to functions ([dcl.init], [class.copy.ctor]), and returned by functions (except objects of classes for which copying or moving has been restricted; see [dcl.fct.def.delete] and [class.access]).

Other plausible operators, such as equality comparison, can be defined by the user; see [over.oper].

— end note]