8.1 KiB
[class.static]
11 Classes [class]
11.4 Class members [class.mem]
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(); // OK, no object necessary g().reschedule(); // g() is called} â 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 this pointer ([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.
[Note 1:
The initializer in the definition of a static data member is in the scope of its class ([basic.scope.class]).
â end note]
[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.
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]
The initialization and destruction of static data members is described in[basic.start.static], [basic.start.dynamic], and [basic.start.term].
â end note]
If a non-volatile non-inline const static data member is of integral or enumeration type, its declaration in the class definition can specify abrace-or-equal-initializer in which everyinitializer-clause that is an assignment-expression is a constant expression ([expr.const]).
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]).
Declarations of other static data members shall not specify a brace-or-equal-initializer.
[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]