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

164 lines
10 KiB
Markdown
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

[class.pre]
# 11 Classes [[class]](./#class)
## 11.1 Preamble [class.pre]
[1](#1)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/classes.tex#L11)
A class is a type[.](#1.sentence-1)
Its name becomes a [*class-name*](#nt:class-name "11.1Preamble[class.pre]") ([[class.name]](class.name "11.3Class names")) within its
scope[.](#1.sentence-2)
[class-name:](#nt:class-name "11.1Preamble[class.pre]")
[*identifier*](lex.name#nt:identifier "5.11Identifiers[lex.name]")
[*simple-template-id*](temp.names#nt:simple-template-id "13.3Names of template specializations[temp.names]")
A [*class-specifier*](#nt:class-specifier "11.1Preamble[class.pre]") or
an [*elaborated-type-specifier*](dcl.type.elab#nt:elaborated-type-specifier "9.2.9.5Elaborated type specifiers[dcl.type.elab]") ([[dcl.type.elab]](dcl.type.elab "9.2.9.5Elaborated type specifiers")) is used to
make a [*class-name*](#nt:class-name "11.1Preamble[class.pre]")[.](#1.sentence-3)
An object of a class consists of a
(possibly empty) sequence of members and base class objects[.](#1.sentence-4)
[class-specifier:](#nt:class-specifier "11.1Preamble[class.pre]")
[*class-head*](#nt:class-head "11.1Preamble[class.pre]") { [*member-specification*](class.mem.general#nt:member-specification "11.4.1General[class.mem.general]")opt }
[class-head:](#nt:class-head "11.1Preamble[class.pre]")
[*class-key*](#nt:class-key "11.1Preamble[class.pre]") [*attribute-specifier-seq*](dcl.attr.grammar#nt:attribute-specifier-seq "9.13.1Attribute syntax and semantics[dcl.attr.grammar]")opt [*class-head-name*](#nt:class-head-name "11.1Preamble[class.pre]") [*class-property-specifier-seq*](#nt:class-property-specifier-seq "11.1Preamble[class.pre]")opt [*base-clause*](class.derived.general#nt:base-clause "11.7.1General[class.derived.general]")opt
[*class-key*](#nt:class-key "11.1Preamble[class.pre]") [*attribute-specifier-seq*](dcl.attr.grammar#nt:attribute-specifier-seq "9.13.1Attribute syntax and semantics[dcl.attr.grammar]")opt [*base-clause*](class.derived.general#nt:base-clause "11.7.1General[class.derived.general]")opt
[class-head-name:](#nt:class-head-name "11.1Preamble[class.pre]")
[*nested-name-specifier*](expr.prim.id.qual#nt:nested-name-specifier "7.5.5.3Qualified names[expr.prim.id.qual]")opt [*class-name*](#nt:class-name "11.1Preamble[class.pre]")
[class-property-specifier-seq:](#nt:class-property-specifier-seq "11.1Preamble[class.pre]")
[*class-property-specifier*](#nt:class-property-specifier "11.1Preamble[class.pre]") [*class-property-specifier-seq*](#nt:class-property-specifier-seq "11.1Preamble[class.pre]")opt
[class-property-specifier:](#nt:class-property-specifier "11.1Preamble[class.pre]")
final
trivially_relocatable_if_eligible
replaceable_if_eligible
[class-key:](#nt:class-key "11.1Preamble[class.pre]")
class
struct
union
A class declaration where the [*class-name*](#nt:class-name "11.1Preamble[class.pre]") in the [*class-head-name*](#nt:class-head-name "11.1Preamble[class.pre]") is a [*simple-template-id*](temp.names#nt:simple-template-id "13.3Names of template specializations[temp.names]") shall be an explicit specialization ([[temp.expl.spec]](temp.expl.spec "13.9.4Explicit specialization")) or
a partial specialization ([[temp.spec.partial]](temp.spec.partial "13.7.6Partial specialization"))[.](#1.sentence-5)
A [*class-specifier*](#nt:class-specifier "11.1Preamble[class.pre]") whose[*class-head*](#nt:class-head "11.1Preamble[class.pre]") omits the[*class-head-name*](#nt:class-head-name "11.1Preamble[class.pre]") defines an [*unnamed class*](#def:class,unnamed "11.1Preamble[class.pre]")[.](#1.sentence-6)
[*Note [1](#note-1)*:
An unnamed class thus can't
be final[.](#1.sentence-7)
— *end note*]
Otherwise, the [*class-name*](#nt:class-name "11.1Preamble[class.pre]") is an [*identifier*](lex.name#nt:identifier "5.11Identifiers[lex.name]");
it is not looked up, and the [*class-specifier*](#nt:class-specifier "11.1Preamble[class.pre]") introduces it[.](#1.sentence-8)
[2](#2)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/classes.tex#L81)
The component name of the[*class-name*](#nt:class-name "11.1Preamble[class.pre]") is also bound in the scope of the class (template)
itself; this is known as the [*injected-class-name*](#def:injected-class-name "11.1Preamble[class.pre]")[.](#2.sentence-1)
For purposes of access checking, the injected-class-name is treated as
if it were a public member name[.](#2.sentence-2)
A [*class-specifier*](#nt:class-specifier "11.1Preamble[class.pre]") is commonly referred to as a [*class
definition*](#def:definition,class "11.1Preamble[class.pre]")[.](#2.sentence-3)
A class is considered defined after the closing brace of its[*class-specifier*](#nt:class-specifier "11.1Preamble[class.pre]") has been seen even though its member
functions are in general not yet defined[.](#2.sentence-4)
The optional [*attribute-specifier-seq*](dcl.attr.grammar#nt:attribute-specifier-seq "9.13.1Attribute syntax and semantics[dcl.attr.grammar]") appertains to the class; the attributes in
the [*attribute-specifier-seq*](dcl.attr.grammar#nt:attribute-specifier-seq "9.13.1Attribute syntax and semantics[dcl.attr.grammar]") are thereafter considered attributes of the class
whenever it is named[.](#2.sentence-5)
[3](#3)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/classes.tex#L97)
If a [*class-head-name*](#nt:class-head-name "11.1Preamble[class.pre]") contains a [*nested-name-specifier*](expr.prim.id.qual#nt:nested-name-specifier "7.5.5.3Qualified names[expr.prim.id.qual]"),
the [*class-specifier*](#nt:class-specifier "11.1Preamble[class.pre]") shall not inhabit a class scope[.](#3.sentence-1)
If its [*class-name*](#nt:class-name "11.1Preamble[class.pre]") is an [*identifier*](lex.name#nt:identifier "5.11Identifiers[lex.name]"),
the [*class-specifier*](#nt:class-specifier "11.1Preamble[class.pre]") shall correspond to
one or more declarations nominable in
the class, class template, or namespace to which the[*nested-name-specifier*](expr.prim.id.qual#nt:nested-name-specifier "7.5.5.3Qualified names[expr.prim.id.qual]") refers;
they shall all have the same target scope, and the target scope of the[*class-specifier*](#nt:class-specifier "11.1Preamble[class.pre]") is that scope[.](#3.sentence-2)
[*Example [1](#example-1)*: namespace N {template<class>struct A {struct B; };}using N::A;template<class T> struct A<T>::B {}; // OKtemplate<> struct A<void> {}; // OK — *end example*]
[4](#4)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/classes.tex#L121)
[*Note [2](#note-2)*:
The [*class-key*](#nt:class-key "11.1Preamble[class.pre]") determines
whether the class is a union ([[class.union]](class.union "11.5Unions")) and
whether access is public or private by default ([[class.access]](class.access "11.8Member access control"))[.](#4.sentence-1)
A union holds the value of at most one data member at a time[.](#4.sentence-2)
— *end note*]
[5](#5)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/classes.tex#L129)
Each [*class-property-specifier*](#nt:class-property-specifier "11.1Preamble[class.pre]") shall appear at most once
within a single [*class-property-specifier-seq*](#nt:class-property-specifier-seq "11.1Preamble[class.pre]")[.](#5.sentence-1)
Whenever a [*class-key*](#nt:class-key "11.1Preamble[class.pre]") is followed
by a [*class-head-name*](#nt:class-head-name "11.1Preamble[class.pre]"),
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*](#nt:class-property-specifier "11.1Preamble[class.pre]")[.](#5.sentence-2)
[*Example [2](#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](#6)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/classes.tex#L153)
If a class is marked with the [*class-property-specifier*](#nt:class-property-specifier "11.1Preamble[class.pre]")final and that class appears as a [*class-or-decltype*](class.derived.general#nt:class-or-decltype "11.7.1General[class.derived.general]") in a [*base-clause*](class.derived.general#nt:base-clause "11.7.1General[class.derived.general]") ([[class.derived]](class.derived "11.7Derived classes")), the program is ill-formed[.](#6.sentence-1)
[7](#7)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/classes.tex#L158)
[*Note [3](#note-3)*:
Complete objects of class type have nonzero size[.](#7.sentence-1)
Base class subobjects and
members declared with the no_unique_address attribute ([[dcl.attr.nouniqueaddr]](dcl.attr.nouniqueaddr "9.13.11No unique address attribute"))
are not so constrained[.](#7.sentence-2)
— *end note*]
[8](#8)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/classes.tex#L166)
[*Note [4](#note-4)*:
Class objects can be assigned ([[over.assign]](over.assign "12.4.3.2Simple assignment"), [[class.copy.assign]](class.copy.assign "11.4.6Copy/move assignment operator")),
passed as arguments to functions ([[dcl.init]](dcl.init "9.5Initializers"), [[class.copy.ctor]](class.copy.ctor "11.4.5.3Copy/move constructors")), and
returned by functions (except objects of classes for which copying or moving has
been restricted; see [[dcl.fct.def.delete]](dcl.fct.def.delete "9.6.3Deleted definitions") and [[class.access]](class.access "11.8Member access control"))[.](#8.sentence-1)
Other plausible operators, such as equality comparison,
can be defined by the user; see [[over.oper]](over.oper "12.4Overloaded operators")[.](#8.sentence-2)
— *end note*]