9 Classes [class]

9.4 Static members [class.static]

A data or function member of a class may be declared static in a class definition, in which case it is a static member of the class.

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:

struct process {
  static void reschedule();
};
process& g();

void f() {
  process::reschedule();        // OK: no object necessary
  g().reschedule();             // g() is called
}

 — end example ]

A static member may be referred to directly in the scope of its class or in the scope of a class derived (Clause [class.derived]) from its class; in this case, the static member is referred to as if a qualified-id expression was used, with the nested-name-specifier of the qualified-id naming the class scope from which the static member is referenced. [ Example:

int g();
struct X {
  static int g();
};
struct Y : X {
  static int i;
};
int Y::i = g();                 // equivalent to Y::g();

 — end example ]

If an unqualified-id ([expr.prim]) is used in the definition of a static member following the member's declarator-id, and name lookup ([basic.lookup.unqual]) finds that the unqualified-id refers to a static member, enumerator, or nested type of the member's class (or of a base class of the member's class), the unqualified-id is transformed into a qualified-id expression in which the nested-name-specifier names the class scope from which the member is referenced. [ Note: See [expr.prim] for restrictions on the use of non-static data members and non-static member functions.  — end note ]

Static members obey the usual class member access rules (Clause [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: It cannot be specified in member declarations that appear in namespace scope.  — end note ]

9.4.1 Static member functions [class.static.mfct]

Note: The rules described in [class.mfct] apply to static member functions.  — end note ]

Note: A static member function does not have a this pointer ([class.this]).  — end note ] A static member function shall not be virtual. There shall not be a static and a non-static member function with the same name and the same parameter types ([over.load]). A static member function shall not be declared const, volatile, or const volatile.

9.4.2 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 declared thread_local there is one copy of the data member that is shared by all the objects of the class.

The declaration of a static data member in its class definition is not a definition and may be of an incomplete type other than cv-qualified void. The definition for a static data member shall appear in a namespace scope enclosing the member's class definition. In the definition at namespace scope, the name of the static data member shall be qualified by its class name using the :: operator. The initializer expression in the definition of a static data member is in the scope of its class ([basic.scope.class]). Example:

class process {
  static process* run_chain;
  static process* running;
};

process* process::running = get_main();
process* process::run_chain = running;

The static data member run_chain of class process is defined in global scope; the notation process::run_chain specifies that the member run_chain is a member of class process and in the scope of class process. In the static data member definition, the initializer expression refers to the static data member running of class process.  — end example ]

Note: Once the static data member has been defined, it exists even if no objects of its class have been created. [ Example: in the example above, run_chain and running exist even if no objects of class process are created by the program.  — end example ]  — end note ]

If a non-volatile const static data member is of integral or enumeration type, its declaration in the class definition can specify a brace-or-equal-initializer in which every initializer-clause that is an assignment-expression is a constant expression ([expr.const]). A static data member of literal type can be declared in the class definition with the constexpr specifier; if so, its declaration shall specify a brace-or-equal-initializer in which every initializer-clause that is an assignment-expression is a constant expression. [ Note: In both these cases, the member may appear in constant expressions.  — end note ] 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.

Note: There shall be exactly one definition of a static data member that is odr-used ([basic.def.odr]) in a program; no diagnostic is required.  — end note ] Unnamed classes and classes contained directly or indirectly within unnamed classes shall not contain static data members.

Static data members of a class in namespace scope have external linkage ([basic.link]). A local class shall not have static data members.

Static data members are initialized and destroyed exactly like non-local variables ([basic.start.init], [basic.start.term]).

A static data member shall not be mutable ([dcl.stc]).