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

183 lines
8.1 KiB
Markdown
Raw 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.static]
# 11 Classes [[class]](./#class)
## 11.4 Class members [[class.mem]](class.mem#class.static)
### 11.4.9 Static members [class.static]
#### [11.4.9.1](#general) General [[class.static.general]](class.static.general)
[1](#general-1)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/classes.tex#L2732)
A static member s of class X may be referred to
using the [*qualified-id*](expr.prim.id.qual#nt:qualified-id "7.5.5.3Qualified names[expr.prim.id.qual]") expression X::s; it is not
necessary to use the class member access syntax ([[expr.ref]](expr.ref "7.6.1.5Class member access")) to
refer to a static member[.](#general-1.sentence-1)
A static member may be
referred to using the class member access syntax, in which case the
object expression is evaluated[.](#general-1.sentence-2)
[*Example [1](#general-example-1)*: struct process {static void reschedule();};
process& g();
void f() { process::reschedule(); // OK, no object necessary g().reschedule(); // g() is called} — *end example*]
[2](#general-2)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/classes.tex#L2753)
Static members obey the usual class member access rules ([[class.access]](class.access "11.8Member access control"))[.](#general-2.sentence-1)
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*](class.mem.general#nt:member-specification "11.4.1General[class.mem.general]") of
the class definition[.](#general-2.sentence-2)
[*Note [1](#general-note-1)*:
It cannot be specified in member declarations that appear in namespace scope[.](#general-2.sentence-3)
— *end note*]
#### [11.4.9.2](#mfct) Static member functions [[class.static.mfct]](class.static.mfct)
[1](#mfct-1)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/classes.tex#L2766)
[*Note [1](#mfct-note-1)*:
The rules described in [[class.mfct]](class.mfct "11.4.2Member functions") apply to static member
functions[.](#mfct-1.sentence-1)
— *end note*]
[2](#mfct-2)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/classes.tex#L2772)
[*Note [2](#mfct-note-2)*:
A static member function does not have a this pointer ([[expr.prim.this]](expr.prim.this "7.5.3This"))[.](#mfct-2.sentence-1)
A static member function cannot be qualified with const,volatile, or virtual ([[dcl.fct]](dcl.fct "9.3.4.6Functions"))[.](#mfct-2.sentence-2)
— *end note*]
#### [11.4.9.3](#data) Static data members [[class.static.data]](class.static.data)
[1](#data-1)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/classes.tex#L2783)
A static data member is not part of the subobjects of a class[.](#data-1.sentence-1)
If a
static data member is declared thread_local there is one copy of
the member per thread[.](#data-1.sentence-2)
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[.](#data-1.sentence-3)
[2](#data-2)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/classes.tex#L2790)
A static data member shall not be mutable ([[dcl.stc]](dcl.stc "9.2.2Storage class specifiers"))[.](#data-2.sentence-1)
A static data member shall not be a direct member ([[class.mem]](class.mem "11.4Class members"))
of an unnamed ([[class.pre]](class.pre "11.1Preamble")) or local ([[class.local]](class.local "11.6Local class declarations")) class or
of a (possibly indirectly) nested class ([[class.nest]](class.nest "11.4.12Nested class declarations")) thereof[.](#data-2.sentence-2)
[3](#data-3)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/classes.tex#L2796)
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[.](#data-3.sentence-1)
[*Note [1](#data-note-1)*:
The [*initializer*](dcl.init.general#nt:initializer "9.5.1General[dcl.init.general]") in the definition of a
static data member is in the scope of its
class ([[basic.scope.class]](basic.scope.class "6.4.7Class scope"))[.](#data-3.sentence-2)
— *end note*]
[*Example [1](#data-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_chain is a member of class process and in the scope of classprocess[.](#data-3.sentence-3)
In the static data member definition, the[*initializer*](dcl.init.general#nt:initializer "9.5.1General[dcl.init.general]") expression refers to the static data
member running of class process[.](#data-3.sentence-4)
— *end example*]
[*Note [2](#data-note-2)*:
Once the static data member has been defined, it exists even if
no objects of its class have been created[.](#data-3.sentence-5)
[*Example [2](#data-example-2)*:
In the example above, run_chain and running exist even
if no objects of class process are created by the program[.](#data-3.sentence-6)
— *end example*]
The initialization and destruction of static data members is described in[[basic.start.static]](basic.start.static "6.10.3.2Static initialization"), [[basic.start.dynamic]](basic.start.dynamic "6.10.3.3Dynamic initialization of non-block variables"), and [[basic.start.term]](basic.start.term "6.10.3.4Termination")[.](#data-3.sentence-7)
— *end note*]
[4](#data-4)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/classes.tex#L2840)
If a non-volatile non-inline const static data member is
of integral or enumeration type,
its declaration in the class definition can specify a[*brace-or-equal-initializer*](dcl.init.general#nt:brace-or-equal-initializer "9.5.1General[dcl.init.general]") in which every[*initializer-clause*](dcl.init.general#nt:initializer-clause "9.5.1General[dcl.init.general]") that is an [*assignment-expression*](expr.assign#nt:assignment-expression "7.6.19Assignment and compound assignment operators[expr.assign]") is a constant expression ([[expr.const]](expr.const "7.7Constant expressions"))[.](#data-4.sentence-1)
The member shall still be defined in a namespace scope if
it is odr-used ([[basic.def.odr]](basic.def.odr#term.odr.use "6.3One-definition rule")) in the program and the
namespace scope definition shall not contain an [*initializer*](dcl.init.general#nt:initializer "9.5.1General[dcl.init.general]")[.](#data-4.sentence-2)
The declaration of an inline static data member (which is a definition)
may specify a [*brace-or-equal-initializer*](dcl.init.general#nt:brace-or-equal-initializer "9.5.1General[dcl.init.general]")[.](#data-4.sentence-3)
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]](depr.static.constexpr "D.7Redeclaration of static constexpr data members"))[.](#data-4.sentence-4)
Declarations of other
static data members shall not specify a [*brace-or-equal-initializer*](dcl.init.general#nt:brace-or-equal-initializer "9.5.1General[dcl.init.general]")[.](#data-4.sentence-5)
[5](#data-5)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/classes.tex#L2857)
[*Note [3](#data-note-3)*:
There is exactly one definition of a static data member
that is odr-used ([[basic.def.odr]](basic.def.odr#term.odr.use "6.3One-definition rule")) in a valid program[.](#data-5.sentence-1)
— *end note*]
[6](#data-6)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/classes.tex#L2863)
[*Note [4](#data-note-4)*:
Static data members of a class in namespace scope have the linkage of the name of the class ([[basic.link]](basic.link "6.7Program and linkage"))[.](#data-6.sentence-1)
— *end note*]