Стандарт C++ 11 (1119564), страница 68
Текст из файла (страница 68)
The optional attribute-specifierseq appertains to the entity being declared. The bit-field attribute is not part of the type of the classmember. The constant-expression shall be an integral constant expression with a value greater than or equalto zero. The value of the integral constant expression may be larger than the number of bits in the objectrepresentation (3.9) of the bit-field’s type; in such cases the extra bits are used as padding bits and do notparticipate in the value representation (3.9) of the bit-field.
Allocation of bit-fields within a class object isimplementation-defined. Alignment of bit-fields is implementation-defined. Bit-fields are packed into someaddressable allocation unit. [ Note: Bit-fields straddle allocation units on some machines and not on others.Bit-fields are assigned right-to-left on some machines, left-to-right on others. — end note ]2A declaration for a bit-field that omits the identifier declares an unnamed bit-field.
Unnamed bit-fieldsare not members and cannot be initialized. [ Note: An unnamed bit-field is useful for padding to conformto externally-imposed layouts. — end note ] As a special case, an unnamed bit-field with a width of zerospecifies alignment of the next bit-field at an allocation unit boundary. Only when declaring an unnamedbit-field may the value of the constant-expression be equal to zero.3A bit-field shall not be a static member. A bit-field shall have integral or enumeration type (3.9.1). It isimplementation-defined whether a plain (neither explicitly signed nor unsigned) char, short, int, long,or long long bit-field is signed or unsigned. A bool value can successfully be stored in a bit-field of anynonzero size.
The address-of operator & shall not be applied to a bit-field, so there are no pointers to bitfields. A non-const reference shall not be bound to a bit-field (8.5.3). [ Note: If the initializer for a referenceof type const T& is an lvalue that refers to a bit-field, the reference is bound to a temporary initialized tohold the value of the bit-field; the reference is not bound to the bit-field directly. See 8.5.3. — end note ]4If the value true or false is stored into a bit-field of type bool of any size (including a one bit bit-field),the original bool value and the value of the bit-field shall compare equal. If the value of an enumerator isstored into a bit-field of the same enumeration type and the number of bits in the bit-field is large enoughto hold all the values of that enumeration type (7.2), the original enumerator value and the value of thebit-field shall compare equal. [ Example:enum BOOL { FALSE=0, TRUE=1 };struct A {BOOL b:1;};A a;void f() {a.b = TRUE;if (a.b == TRUE){ /∗ ...
∗/ }}// yields true— end example ]9.71Nested class declarations[class.nest]A class can be declared within another class. A class declared within another is called a nested class. Thename of a nested class is local to its enclosing class. The nested class is in the scope of its enclosing class.§ 9.7© ISO/IEC 2011 – All rights reserved229ISO/IEC 14882:2011(E)[ Note: See 5.1 for restrictions on the use of non-static data members and non-static member functions.— end note ][ Example:int x;int y;struct enclose {int x;static int s;struct inner {void f(int i) {int a = sizeof(x);//x = i;//s = i;//::x = i;//y = i;//}void g(enclose* p, int i) {p->x = i;//}};};OK: operand of sizeof is an unevaluated operanderror: assign to enclose::xOK: assign to enclose::sOK: assign to global xOK: assign to global yOK: assign to enclose::x// error: inner not in scopeinner* p = 0;— end example ]2Member functions and static data members of a nested class can be defined in a namespace scope enclosingthe definition of their class.
[ Example:struct enclosestruct innerstatic intvoid f(int};};{{x;i);int enclose::inner::x = 1;void enclose::inner::f(int i) { /∗ ... ∗/ }— end example ]3If class X is defined in a namespace scope, a nested class Y may be declared in class X and later defined in thedefinition of class X or be later defined in a namespace scope enclosing the definition of class X. [ Example:class E {class I1;class I2;class I1 { };};class E::I2 { };// forward declaration of nested class// definition of nested class// definition of nested class— end example ]§ 9.7230© ISO/IEC 2011 – All rights reservedISO/IEC 14882:2011(E)4Like a member function, a friend function (11.3) defined within a nested class is in the lexical scope of thatclass; it obeys the same rules for name binding as a static member function of that class (9.4), but it has nospecial access rights to members of an enclosing class.9.81Local class declarations[class.local]A class can be declared within a function definition; such a class is called a local class.
The name of a localclass is local to its enclosing scope. The local class is in the scope of the enclosing scope, and has the sameaccess to names outside the function as does the enclosing function. Declarations in a local class shall notodr-use (3.2) a variable with automatic storage duration from an enclosing scope. [ Example:int x;void f() {static int s ;int x;const int N = 5;extern int q();struct local {int g() { return x; }int h() { return s; }int k() { return ::x; }int l() { return q(); }int m() { return N; }int *n() { return &N; }};////////////error: odr-use of automatic variable xOKOKOKOK: not an odr-useerror: odr-use of automatic variable N}local* p = 0;// error: local not in scope— end example ]2An enclosing function has no special access to members of the local class; it obeys the usual access rules(Clause 11).
Member functions of a local class shall be defined within their class definition, if they aredefined at all.3If class X is a local class a nested class Y may be declared in class X and later defined in the definition ofclass X or be later defined in the same scope as the definition of class X. A class nested within a local classis a local class.4A local class shall not have static data members.9.91Nested type names[class.nested.type]Type names obey exactly the same scope rules as other names. In particular, type names defined within aclass definition cannot be used outside their class without qualification.
[ Example:struct X {typedef int I;class Y { /∗ ... ∗/ };I a;};I b;Y c;X::Y d;X::I e;////////errorerrorOKOK§ 9.9© ISO/IEC 2011 – All rights reserved231ISO/IEC 14882:2011(E)— end example ]§ 9.9232© ISO/IEC 2011 – All rights reservedISO/IEC 14882:2011(E)101Derived classes[class.derived]A list of base classes can be specified in a class definition using the notation:base-clause:: base-specifier-listbase-specifier-list:base-specifier ...optbase-specifier-list , base-specifier ...optbase-specifier:attribute-specifier-seqopt base-type-specifierattribute-specifier-seqopt virtual access-specifieropt base-type-specifierattribute-specifier-seqopt access-specifier virtualopt base-type-specifierclass-or-decltype:nested-name-specifieropt class-namedecltype-specifierbase-type-specifier:class-or-decltypeaccess-specifier:privateprotectedpublicThe optional attribute-specifier-seq appertains to the base-specifier.2The type denoted by a base-type-specifier shall be a class type that is not an incompletely defined class(Clause 9); this class is called a direct base class for the class being defined.
During the lookup for a baseclass name, non-type names are ignored (3.3.10). If the name found is not a class-name, the program isill-formed. A class B is a base class of a class D if it is a direct base class of D or a direct base class of one ofD’s base classes. A class is an indirect base class of another if it is a base class but not a direct base class.A class is said to be (directly or indirectly) derived from its (direct or indirect) base classes.
[ Note: SeeClause 11 for the meaning of access-specifier. — end note ] Unless redeclared in the derived class, membersof a base class are also considered to be members of the derived class. The base class members are said tobe inherited by the derived class. Inherited members can be referred to in expressions in the same manneras other members of the derived class, unless their names are hidden or ambiguous (10.2). [ Note: The scoperesolution operator :: (5.1) can be used to refer to a direct or indirect base member explicitly.
This allowsaccess to a name that has been redeclared in the derived class. A derived class can itself serve as a base classsubject to access control; see 11.2. A pointer to a derived class can be implicitly converted to a pointer toan accessible unambiguous base class (4.10). An lvalue of a derived class type can be bound to a referenceto an accessible unambiguous base class (8.5.3). — end note ]3The base-specifier-list specifies the type of the base class subobjects contained in an object of the derivedclass type. [ Example:struct Base {int a, b, c;};struct Derived : Base {int b;© ISO/IEC 2011 – All rights reserved233ISO/IEC 14882:2011(E)};struct Derived2 : Derived {int c;};Here, an object of class Derived2 will have a subobject of class Derived which in turn will have a subobjectof class Base.