[class.static] (original) (raw)

11 Classes [class]

11.4 Class members [class.mem]

11.4.8 Static members [class.static]

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();
g().reschedule();
}

end example

]

A static member may be referred to directly in the scope of its class or in the scope of a class derived ([class.derived]) from its class; in this case, the static member is referred to as if a qualified-id expression was used, with thenested-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();

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

:

It cannot be specified in member declarations that appear in namespace scope.

end note

]

11.4.8.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 thispointer ([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.

11.4.8.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 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]).

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.

The definition for a static data member that is not defined inline in the class definition 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.

[ 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 classprocess is defined in global scope; the notationprocess​::​run_­chain specifies that the member run_­chainis 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

:

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

]

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.

An inline static data member may be defined in the class definition and 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]).

[ Note

:

There is exactly one definition of a static data member that is odr-used ([basic.def.odr]) in a valid program.

end note

]

[ Note

:

Static data members of a class in namespace scope have the linkage of the name of the class ([basic.link]).

end note

]