Стандарт C++ 11 (1119564), страница 67
Текст из файла (страница 67)
[ Note: See 5.1 for restrictions on the use of non-static data members and non-static memberfunctions. — end note ]5Static members obey the usual class member access rules (Clause 11). When used in the declaration ofa class member, the static specifier shall only be used in the member declarations that appear withinthe member-specification of the class definition. [ Note: It cannot be specified in member declarations thatappear in namespace scope. — end note ]9.4.1Static member functions[class.static.mfct]1[ Note: The rules described in 9.3 apply to static member functions.
— end note ]2[ Note: A static member function does not have a this pointer (9.3.2). — end note ] A static memberfunction shall not be virtual. There shall not be a static and a non-static member function with thesame name and the same parameter types (13.1). A static member function shall not be declared const,volatile, or const volatile.9.4.2Static data members[class.static.data]1A static data member is not part of the subobjects of a class. If a static data member is declaredthread_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.2The declaration of a static data member in its class definition is not a definition and may be of an incompletetype other than cv-qualified void. The definition for a static data member shall appear in a namespacescope enclosing the member’s class definition. In the definition at namespace scope, the name of the staticdata member shall be qualified by its class name using the :: operator. The initializer expression in thedefinition of a static data member is in the scope of its class (3.3.7).
[ Example:class process {static process* run_chain;static process* running;};process* process::running = get_main();§ 9.4.2226© ISO/IEC 2011 – All rights reservedISO/IEC 14882:2011(E)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 classprocess.
In the static data member definition, the initializer expression refers to the static data memberrunning of class process. — end example ][ Note: Once the static data member has been defined, it exists even if no objects of its class have beencreated. [ Example: in the example above, run_chain and running exist even if no objects of class processare created by the program. — end example ] — end note ]3If a non-volatile const static data member is of integral or enumeration type, its declaration in the classdefinition can specify a brace-or-equal-initializer in which every initializer-clause that is an assignmentexpression is a constant expression (5.19). A static data member of literal type can be declared in theclass definition with the constexpr specifier; if so, its declaration shall specify a brace-or-equal-initializerin which every initializer-clause that is an assignment-expression is a constant expression.
[ Note: In boththese cases, the member may appear in constant expressions. — end note ] The member shall still be definedin a namespace scope if it is odr-used (3.2) in the program and the namespace scope definition shall notcontain an initializer.4[ Note: There shall be exactly one definition of a static data member that is odr-used (3.2) in a program;no diagnostic is required.
— end note ] Unnamed classes and classes contained directly or indirectly withinunnamed classes shall not contain static data members.5Static data members of a class in namespace scope have external linkage (3.5). A local class shall not havestatic data members.6Static data members are initialized and destroyed exactly like non-local variables (3.6.2, 3.6.3).7A static data member shall not be mutable (7.1.1).9.5Unions[class.union]1In a union, at most one of the non-static data members can be active at any time, that is, the value of atmost one of the non-static data members can be stored in a union at any time. [ Note: One special guaranteeis made in order to simplify the use of unions: If a standard-layout union contains several standard-layoutstructs that share a common initial sequence (9.2), and if an object of this standard-layout union typecontains one of the standard-layout structs, it is permitted to inspect the common initial sequence of any ofstandard-layout struct members; see 9.2.
— end note ] The size of a union is sufficient to contain the largestof its non-static data members. Each non-static data member is allocated as if it were the sole member of astruct.2A union can have member functions (including constructors and destructors), but not virtual (10.3) functions.A union shall not have base classes.
A union shall not be used as a base class. If a union contains a non-staticdata member of reference type the program is ill-formed. At most one non-static data member of a union mayhave a brace-or-equal-initializer. [ Note: If any non-static data member of a union has a non-trivial defaultconstructor (12.1), copy constructor (12.8), move constructor (12.8), copy assignment operator (12.8), moveassignment operator (12.8), or destructor (12.4), the corresponding member function of the union must beuser-provided or it will be implicitly deleted (8.4.3) for the union.
— end note ]3[ Example: Consider the following union:union U {int i;float f;std::string s;§ 9.5© ISO/IEC 2011 – All rights reserved227ISO/IEC 14882:2011(E)};Since std::string (21.3) declares non-trivial versions of all of the special member functions, U will havean implicitly deleted default constructor, copy/move constructor, copy/move assignment operator, and destructor. To use U, some or all of these member functions must be user-provided. — end example ]4[ Note: In general, one must use explicit destructor calls and placement new operators to change the activemember of a union. — end note ] [ Example: Consider an object u of a union type U having non-static datamembers m of type M and n of type N.
If M has a non-trivial destructor and N has a non-trivial constructor(for instance, if they declare or inherit virtual functions), the active member of u can be safely switchedfrom m to n using the destructor and placement new operator as follows:u.m.~M();new (&u.n) N;— end example ]5A union of the formunion { member-specification } ;is called an anonymous union; it defines an unnamed object of unnamed type. The member-specification ofan anonymous union shall only define non-static data members.
[ Note: Nested types and functions cannotbe declared within an anonymous union. — end note ] The names of the members of an anonymous unionshall be distinct from the names of any other entity in the scope in which the anonymous union is declared.For the purpose of name lookup, after the anonymous union definition, the members of the anonymous unionare considered to have been defined in the scope in which the anonymous union is declared. [ Example:void f() {union { int a; const char* p; };a = 1;p = "Jennifer";}Here a and p are used like ordinary (nonmember) variables, but since they are union members they havethe same address. — end example ]6Anonymous unions declared in a named namespace or in the global namespace shall be declared static.Anonymous unions declared at block scope shall be declared with any storage class allowed for a block-scopevariable, or with no storage class.
A storage class is not allowed in a declaration of an anonymous unionin a class scope. An anonymous union shall not have private or protected members (Clause 11). Ananonymous union shall not have function members.7A union for which objects or pointers are declared is not an anonymous union. [ Example:union { int aa; char* p; } obj, *ptr = &obj;aa = 1;// errorptr->aa = 1;// OKThe assignment to plain aa is ill-formed since the member name is not visible outside the union, and evenif it were visible, it is not associated with any particular object. — end example ] [ Note: Initialization ofunions with no user-declared constructors is described in (8.5.1). — end note ]8A union-like class is a union or a class that has an anonymous union as a direct member. A union-likeclass X has a set of variant members. If X is a union its variant members are the non-static data members;§ 9.5228© ISO/IEC 2011 – All rights reservedISO/IEC 14882:2011(E)otherwise, its variant members are the non-static data members of all anonymous unions that are membersof X.9.61Bit-fields[class.bit]A member-declarator of the formidentifieropt attribute-specifier-seqopt : constant-expressionspecifies a bit-field; its length is set off from the bit-field name by a colon.