9 - Classes [class]

-1- A class is a type. Its name becomes a class-name (class.name) within its scope.

class-name:
	identifier
	template-id
Class-specifiers and elaborated-type-specifiers (dcl.type.elab) are used to make class-names. An object of a class consists of a (possibly empty) sequence of members and base class objects.
class-specifier:
	class-head { member-specificationopt }
class-head:
	class-key identifieropt base-clauseopt
	class-key nested-name-specifier identifier base-clauseopt
	class-key nested-name-specifieropt template-id base-clauseopt
class-key:
	class
	struct
	union

-2- A class-name is inserted into the scope in which it is declared immediately after the class-name is seen. The class-name is also inserted into the scope of the class itself. For purposes of access checking, the inserted class name is treated as if it were a public member name. A class-specifier is commonly referred to as a class definition. A class is considered defined after the closing brace of its class-specifier has been seen even though its member functions are in general not yet defined.

-3- Complete objects and member subobjects of class type shall have nonzero size.*

[Footnote: Base class subobjects are not so constrained. --- end foonote]
[Note: class objects can be assigned, passed as arguments to functions, and returned by functions (except objects of classes for which copying has been restricted; see class.copy). Other plausible operators, such as equality comparison, can be defined by the user; see over.oper. ]

-4- A structure is a class defined with the class-key struct; its members and base classes (clause class.derived) are public by default (clause class.access). A union is a class defined with the class-key union; its members are public by default and it holds only one data member at a time (class.union). [Note: aggregates of class type are described in dcl.init.aggr. ] A POD-struct is an aggregate class that has no non-static data members of type pointer to member, non-POD-struct, non-POD-union (or array of such types) or reference, and has no user-defined copy assignment operator and no user-defined destructor. Similarly, a POD-union is an aggregate union that has no non-static data members of type pointer to member, non-POD-struct, non-POD-union (or array of such types) or reference, and has no user-defined copy assignment operator and no user-defined destructor. A POD class is a class that is either a POD-struct or a POD-union.

9.1 - Class names [class.name]

-1- A class definition introduces a new type. [Example:

struct X { int a; };
struct Y { int a; };
X a1;
Y a2;
int a3;
declares three variables of three different types. This implies that
a1 = a2;                        //  error:  Y  assigned to  X
a1 = a3;                        //  error:  int  assigned to  X
are type mismatches, and that
int f(X);
int f(Y);
declare an overloaded (clause over) function f() and not simply a single function f() twice. For the same reason,
struct S { int a; };
struct S { int a; };            //  error, double definition
is ill-formed because it defines S twice. ]

-2- A class definition introduces the class name into the scope where it is defined and hides any class, object, function, or other declaration of that name in an enclosing scope (basic.scope). If a class name is declared in a scope where an object, function, or enumerator of the same name is also declared, then when both declarations are in scope, the class can be referred to only using an elaborated-type-specifier (basic.lookup.elab). [Example:

struct stat {
	//  ...
};
stat gstat;                     //  use plain  stat  to
				//  define variable

int stat(struct stat*);         //  redeclare  stat  as function
void f()
{
    struct stat* ps;            //   struct  prefix needed
				//  to name  struct   stat
				//  ...
    stat(ps);                   //  call  stat()
				//  ...
}

--- end example] A declaration consisting solely of class-key identifier ; is either a redeclaration of the name in the current scope or a forward declaration of the identifier as a class name. It introduces the class name into the current scope. [Example:
struct s { int a; };

void g()
{
    struct s;                   //  hide global  struct   s
				//   with a local declaration
    s* p;                       //  refer to local  struct   s
    struct s { char* p; };      //  define local  struct   s
    struct s;                   //  redeclaration, has no effect
}

--- end example] [Note: Such declarations allow definition of classes that refer to each other. [Example:
class Vector;

class Matrix {
	//  ...
	friend Vector operator*(Matrix&, Vector&);
};
class Vector {
	//  ...
	friend Vector operator*(Matrix&, Vector&);
};
Declaration of friends is described in class.friend, operator functions in over.oper. ] ]

-3- An elaborated-type-specifier (dcl.type.elab) can also be used as a type-specifier as part of a declaration. It differs from a class declaration in that if a class of the elaborated name is in scope the elaborated name will refer to it. [Example:

struct s { int a; };

void g(int s)
{
	struct s* p = new struct s;     //  global  s
	p->a = s;                       //  local  s
}

--- end example]

-4- [Note: The declaration of a class name takes effect immediately after the identifier is seen in the class definition or elaborated-type-specifier. For example,

class A * A;
first specifies A to be the name of a class and then redefines it as the name of a pointer to an object of that class. This means that the elaborated form class A must be used to refer to the class. Such artistry with names can be confusing and is best avoided. ]

-5- A typedef-name (dcl.typedef) that names a class is a class-name, but shall not be used in an elaborated-type-specifier; see also dcl.typedef.

9.2 - Class members [class.mem]

member-specification:
	member-declaration member-specificationopt
	access-specifier : member-specificationopt
member-declaration:
	decl-specifier-seqopt member-declarator-listopt ;
	function-definition ;opt
	::opt nested-name-specifier templateopt unqualified-id ;
	using-declaration
	template-declaration
member-declarator-list:
	member-declarator
	member-declarator-list , member-declarator
member-declarator:
	declarator pure-specifieropt
	declarator constant-initializeropt
	identifieropt : constant-expression
pure-specifier:
	 = 0
constant-initializer:
	 = constant-expression

-1- The member-specification in a class definition declares the full set of members of the class; no member can be added elsewhere. Members of a class are data members, member functions (class.mfct), nested types, and enumerators. Data members and member functions are static or nonstatic; see class.static. Nested types are classes (class.name, class.nest) and enumerations (dcl.enum) defined in the class, and arbitrary types declared as members by use of a typedef declaration (dcl.typedef). The enumerators of an enumeration (dcl.enum) defined in the class are members of the class. Except when used to declare friends (class.friend) or to introduce the name of a member of a base class into a derived class (namespace.udecl,class.access.dcl), member-declarations declare members of the class, and each such member-declaration shall declare at least one member name of the class. A member shall not be declared twice in the member-specification, except that a nested class or member class template can be declared and then later defined.

-2- A class is considered a completely-defined object type (basic.types) (or complete type) at the closing } of the class-specifier. Within the class member-specification, the class is regarded as complete within function bodies, default arguments and constructor ctor-initializers (including such things in nested classes). Otherwise it is regarded as incomplete within its own class member-specification.

-3- [Note: a single name can denote several function members provided their types are sufficiently different (clause over). ]

-4- A member-declarator can contain a constant-initializer only if it declares a static member (class.static) of integral or enumeration type, see class.static.data.

-5- A member can be initialized using a constructor; see class.ctor. [Note: see clause special for a description of constructors and other special member functions. ]

-6- A member shall not be auto, extern, or register.

-7- The decl-specifier-seq is omitted in constructor, destructor, and conversion function declarations only. The member-declarator-list can be omitted only after a class-specifier, an enum-specifier, or a decl-specifier-seq of the form friend elaborated-type-specifier. A pure-specifier shall be used only in the declaration of a virtual function (class.virtual).

-8- Non-static (class.static) members that are class objects shall be objects of previously defined classes. In particular, a class cl shall not contain an object of class cl, but it can contain a pointer or reference to an object of class cl. When an array is used as the type of a nonstatic member all dimensions shall be specified.

-9- Except when used to form a pointer to member (expr.unary.op), when used in the body of a nonstatic member function of its class or of a class derived from its class (class.mfct.nonstatic), or when used in a mem-initializer for a constructor for its class or for a class derived from its class (class.base.init), a nonstatic data or function member of a class shall only be referred to with the class member access syntax (expr.ref).

-10- [Note: the type of a nonstatic member function is an ordinary function type, and the type of a nonstatic data member is an ordinary object type. There are no special member function types or data member types. ]

-11- [Example: A simple example of a class definition is

struct tnode {
    char tword[20];
    int count;
    tnode *left;
    tnode *right;
};
which contains an array of twenty characters, an integer, and two pointers to similar structures. Once this definition has been given, the declaration
tnode s, *sp;
declares s to be a tnode and sp to be a pointer to a tnode. With these declarations, sp->count refers to the count member of the structure to which sp points; s.left refers to the left subtree pointer of the structure s; and s.right->tword[0] refers to the initial character of the tword member of the right subtree of s. ]

-12- Nonstatic data members of a (non-union) class declared without an intervening access-specifier are allocated so that later members have higher addresses within a class object. The order of allocation of nonstatic data members separated by an access-specifier is unspecified (class.access.spec). Implementation alignment requirements might cause two adjacent members not to be allocated immediately after each other; so might requirements for space for managing virtual functions (class.virtual) and virtual base classes (class.mi).

-13- If T is the name of a class, then each of the following shall have a name different from T:

-14- Two POD-struct (clause class) types are layout-compatible if they have the same number of members, and corresponding members (in order) have layout-compatible types (basic.types).

-15- Two POD-union (clause class) types are layout-compatible if they have the same number of members, and corresponding members (in any order) have layout-compatible types (basic.types).

-16- If a POD-union contains two or more POD-structs that share a common initial sequence, and if the POD-union object currently contains one of these POD-structs, it is permitted to inspect the common initial part of any of them. Two POD-structs share a common initial sequence if corresponding members have layout-compatible types (and, for bit-fields, the same widths) for a sequence of one or more initial members.

-17- A pointer to a POD-struct object, suitably converted using a reinterpret_cast, points to its initial member (or if that member is a bit-field, then to the unit in which it resides) and vice versa. [Note: There might therefore be unnamed padding within a POD-struct object, but not at its beginning, as necessary to achieve appropriate alignment. ]

9.3 - Member functions [class.mfct]

-1- Functions declared in the definition of a class, excluding those declared with a friend specifier (class.friend), are called member functions of that class. A member function may be declared static in which case it is a static member function of its class (class.static); otherwise it is a nonstatic member function of its class (class.mfct.nonstatic, class.this).

-2- A member function may be defined (dcl.fct.def) in its class definition, in which case it is an inline member function (dcl.fct.spec), or it may be defined outside of its class definition if it has already been declared but not defined in its class definition. A member function definition that appears outside of the class definition shall appear in a namespace scope enclosing the class definition. Except for member function definitions that appear outside of a class definition, and except for explicit specializations of template member functions (temp.spec) appearing outside of the class definition, a member function shall not be redeclared.

-3- An inline member function (whether static or nonstatic) may also be defined outside of its class definition provided either its declaration in the class definition or its definition outside of the class definition declares the function as inline. [Note: member functions of a class in namespace scope have external linkage. Member functions of a local class (class.local) have no linkage. See basic.link. ]

-4- There shall be at most one definition of a non-inline member function in a program; no diagnostic is required. There may be more than one inline member function definition in a program. See basic.def.odr and dcl.fct.spec.

-5- If the definition of a member function is lexically outside its class definition, the member function name shall be qualified by its class name using the :: operator. [Note: a name used in a member function definition (that is, in the parameter-declaration-clause including the default arguments (dcl.fct.default), or in the member function body, or, for a constructor function (class.ctor), in a mem-initializer expression (class.base.init)) is looked up as described in basic.lookup. ] [Example:

struct X {
	typedef int T;
	static T count;
	void f(T);
};
void X::f(T t = count) { }
The member function f of class X is defined in global scope; the notation X::f specifies that the function f is a member of class X and in the scope of class X. In the function definition, the parameter type T refers to the typedef member T declared in class X and the default argument count refers to the static data member count declared in class X. ]

-6- A static local variable in a member function always refers to the same object, whether or not the member function is inline.

-7- Member functions may be mentioned in friend declarations after their class has been defined.

-8- Member functions of a local class shall be defined inline in their class definition, if they are defined at all.

-9- [Note: a member function can be declared (but not defined) using a typedef for a function type. The resulting member function has exactly the same type as it would have if the function declarator were provided explicitly, see dcl.fct. For example,

typedef void fv(void);
typedef void fvc(void) const;
struct S {
	fv memfunc1;            //  equivalent to:  void   memfunc1(void);
	void memfunc2();
	fvc memfunc3;           //  equivalent to:  void   memfunc3(void)   const;
};
fv  S::* pmfv1 = &S::memfunc1;
fv  S::* pmfv2 = &S::memfunc2;
fvc S::* pmfv3 = &S::memfunc3;
Also see temp.arg. ]

9.3.1 - Nonstatic member functions [class.mfct.nonstatic]

-1- A nonstatic member function may be called for an object of its class type, or for an object of a class derived (clause class.derived) from its class type, using the class member access syntax (expr.ref, over.match.call). A nonstatic member function may also be called directly using the function call syntax (expr.call, over.match.call)

If a nonstatic member function of a class X is called for an object that is not of type X, or of a type derived from X, the behavior is undefined.

-2- When an id-expression (expr.prim) that is not part of a class member access syntax (expr.ref) and not used to form a pointer to member (expr.unary.op) is used in the body of a nonstatic member function of class X or used in the mem-initializer for a constructor of class X, if name lookup (basic.lookup.unqual) resolves the name in the id-expression to a nonstatic nontype member of class X or of a base class of X, the id-expression is transformed into a class member access expression (expr.ref) using (*this) (class.this) as the postfix-expression to the left of the . operator. The member name then refers to the member of the object for which the function is called. Similarly during name lookup, when an unqualified-id (expr.prim) used in the definition of a member function for class X resolves to a static member, an enumerator or a nested type of class X or of a base class of X, the unqualified-id is transformed into a qualified-id (expr.prim) in which the nested-name-specifier names the class of the member function. [Example:

struct tnode {
	char tword[20];
	int count;
	tnode *left;
	tnode *right;
	void set(char*, tnode* l, tnode* r);
};
void tnode::set(char* w, tnode* l, tnode* r)
{
	count = strlen(w)+1;
	if (sizeof(tword)<=count)
		perror("tnode string too long");
	strcpy(tword,w);
	left = l;
	right = r;
}
void f(tnode n1, tnode n2)
{
	n1.set("abc",&n2,0);
	n2.set("def",0,0);
}
In the body of the member function tnode::set, the member names tword, count, left, and right refer to members of the object for which the function is called. Thus, in the call n1.set("abc",&n2,0), tword refers to n1.tword, and in the call n2.set("def",0,0), it refers to n2.tword. The functions strlen, perror, and strcpy are not members of the class tnode and should be declared elsewhere.*
[Footnote: See, for example, <cstring> (lib.c.strings). --- end foonote]
]

-3- A nonstatic member function may be declared const, volatile, or const volatile. These cv-qualifiers affect the type of the this pointer (class.this). They also affect the function type (dcl.fct) of the member function; a member function declared const is a const member function, a member function declared volatile is a volatile member function and a member function declared const volatile is a const volatile member function. [Example:

struct X {
	void g() const;
	void h() const volatile;
};
X::g is a const member function and X::h is a const volatile member function. ]

-4- A nonstatic member function may be declared virtual (class.virtual) or pure virtual (class.abstract).

9.3.2 - The this pointer [class.this]

-1- In the body of a nonstatic (class.mfct) member function, the keyword this is a non-lvalue expression whose value is the address of the object for which the function is called. The type of this in a member function of a class X is X*. If the member function is declared const, the type of this is const X*, if the member function is declared volatile, the type of this is volatile X*, and if the member function is declared const volatile, the type of this is const volatile X*.

-2- In a const member function, the object for which the function is called is accessed through a const access path; therefore, a const member function shall not modify the object and its non-static data members. [Example:

struct s {
    int a;
    int f() const;
    int g() { return a++; }
    int h() const { return a++; }       //  error
};

int s::f() const { return a; }
The a++ in the body of s::h is ill-formed because it tries to modify (a part of) the object for which s::h() is called. This is not allowed in a const member function because this is a pointer to const; that is, *this has const type. ]

-3- Similarly, volatile semantics (dcl.type.cv) apply in volatile member functions when accessing the object and its non-static data members.

-4- A cv-qualified member function can be called on an object-expression (expr.ref) only if the object-expression is as cv-qualified or less-cv-qualified than the member function. [Example:

void k(s& x, const s& y)
{
    x.f();
    x.g();
    y.f();
    y.g();                      //  error
}
The call y.g() is ill-formed because y is const and s::g() is a non-const member function, that is, s::g() is less-qualified than the object-expression y. ]

-5- Constructors (class.ctor) and destructors (class.dtor) shall not be declared const, volatile or const volatile. [Note: However, these functions can be invoked to create and destroy objects with cv-qualified types, see (class.ctor) and (class.dtor). ]

9.4 - Static members [class.static]

-1- 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.

-2- 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 always evaluated. [Example:

class process {
public:
	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]

-3- 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. The definition of a static member shall not use directly the names of the nonstatic members of its class or of a base class of its class (including as operands of the sizeof operator). The definition of a static member may only refer to these members to form pointer to members (expr.unary.op) or with the class member access syntax (expr.ref).

-4- 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 declaration. [Note: it cannot be specified in member declarations that appear in namespace scope. ]

9.4.1 - Static member functions [class.static.mfct]

-1- [Note: the rules described in class.mfct apply to static member functions. ]

-2- [Note: a static member function does not have a this pointer (class.this). ] A static member function shall not be virtual. There shall not be a static and a nonstatic 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]

-1- A static data member is not part of the subobjects of a class. There is only one copy of a static data member shared by all the objects of the class.

-2- 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. ]

-3- [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. ] ]

-4- If a static data member is of const integral or const enumeration type, its declaration in the class definition can specify a constant-initializer which shall be an integral constant expression (expr.const). In that case, the member can appear in integral constant expressions within its scope. The member shall still be defined in a namespace scope if it is used in the program and the namespace scope definition shall not contain an initializer.

-5- There shall be exactly one definition of a static data member that is used in a program; no diagnostic is required; see basic.def.odr. Unnamed classes and classes contained directly or indirectly within unnamed classes shall not contain static data members. [Note: this is because there is no mechanism to provide the definitions for such static data members. ]

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

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

-8- A static data member shall not be mutable (dcl.stc).

9.5 - Unions [class.union]

-1- In a union, at most one of the data members can be active at any time, that is, the value of at most one of the data members can be stored in a union at any time. [Note: one special guarantee is made in order to simplify the use of unions: If a POD-union contains several POD-structs that share a common initial sequence (class.mem), and if an object of this POD-union type contains one of the POD-structs, it is permitted to inspect the common initial sequence of any of POD-struct members; see class.mem. ] The size of a union is sufficient to contain the largest of its data members. Each data member is allocated as if it were the sole member of a struct. A union can have member functions (including constructors and destructors), but not virtual (class.virtual) functions. A union shall not have base classes. A union shall not be used as a base class. An object of a class with a non-trivial constructor (class.ctor), a non-trivial copy constructor (class.copy), a non-trivial destructor (class.dtor), or a non-trivial copy assignment operator (over.ass, class.copy) cannot be a member of a union, nor can an array of such objects. If a union contains a static data member, or a member of reference type, the program is ill-formed.

-2- A union of the form

union { member-specification } ;
is called an anonymous union; it defines an unnamed object of unnamed type. The member-specification of an anonymous union shall only define non-static data members. [Note: nested types and functions cannot be declared within an anonymous union. ] The names of the members of an anonymous union shall 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 union are considered to have been defined in the scope in which the anonymous union is declared. [Example:
void f()
{
    union { int a; char* p; };
    a = 1;
    //  ...
    p = "Jennifer";
    //  ...
}
Here a and p are used like ordinary (nonmember) variables, but since they are union members they have the same address. ]

-3- Anonymous 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-scope variable, or with no storage class. A storage class is not allowed in a declaration of an anonymous union in a class scope. An anonymous union shall not have private or protected members (clause class.access). An anonymous union shall not have function members.

-4- A union for which objects or pointers are declared is not an anonymous union. [Example:

union { int aa; char* p; } obj, *ptr = &obj;
aa = 1;                         //  error
ptr->aa = 1;                    //  OK
The assignment to plain aa is ill formed since the member name is not visible outside the union, and even if it were visible, it is not associated with any particular object. ] [Note: Initialization of unions with no user-declared constructors is described in (dcl.init.aggr). ]

9.6 - Bit-fields [class.bit]

-1- A member-declarator of the form

identifieropt : constant-expression
specifies a bit-field; its length is set off from the bit-field name by a colon. The bit-field attribute is not part of the type of the class member. The constant-expression shall be an integral constant-expression with a value greater than or equal to zero. The constant-expression may be larger than the number of bits in the object representation (basic.types) of the bit-field's type; in such cases the extra bits are used as padding bits and do not participate in the value representation (basic.types) of the bit-field. Allocation of bit-fields within a class object is implementation-defined. Alignment of bit-fields is implementation-defined. Bit-fields are packed into some addressable 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. ]

-2- A declaration for a bit-field that omits the identifier declares an unnamed bit-field. Unnamed bit-fields are not members and cannot be initialized. [Note: an unnamed bit-field is useful for padding to conform to externally-imposed layouts. ] As a special case, an unnamed bit-field with a width of zero specifies alignment of the next bit-field at an allocation unit boundary. Only when declaring an unnamed bit-field may the constant-expression be a value equal to zero.

-3- A bit-field shall not be a static member. A bit-field shall have integral or enumeration type (basic.fundamental). It is implementation-defined whether a plain (neither explicitly signed nor unsigned) char, short, int or long bit-field is signed or unsigned. A bool value can successfully be stored in a bit-field of any nonzero size. The address-of operator & shall not be applied to a bit-field, so there are no pointers to bit-fields. A non-const reference shall not be bound to a bit-field (dcl.init.ref). [Note: if the initializer for a reference of type const T& is an lvalue that refers to a bit-field, the reference is bound to a temporary initialized to hold the value of the bit-field; the reference is not bound to the bit-field directly. See dcl.init.ref. ]

-4- If 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 is stored into a bit-field of the same enumeration type and the number of bits in the bit-field is large enough to hold all the values of that enumeration type, the original enumerator value and the value of the bit-field shall compare equal. [Example:

enum BOOL { f=0, t=1 };
struct A {
	BOOL b:1;
};
A a;
void f() {
	a.b = t;
	if (a.b == t)           //  shall yield  true
	{ /* ... */ }
}

--- end example]

9.7 - Nested class declarations [class.nest]

-1- A class can be defined within another class. A class defined within another is called a nested class. The name of a nested class is local to its enclosing class. The nested class is in the scope of its enclosing class. Except by using explicit pointers, references, and object names, declarations in a nested class can use only type names, static members, and enumerators from the enclosing class. [Example:

int x;
int y;

class enclose {
public:
    int x;
    static int s;
    class inner {
	void f(int i)
	{
	    int a = sizeof(x);  //  error: refers to  enclose::x
	    x = i;              //  error: assign to  enclose::x
	    s = i;              //  OK: assign to  enclose::s
	    ::x = i;            //  OK: assign to global  x
	    y = i;              //  OK: assign to global  y
	}
	void g(enclose* p, int i)
	{
	    p->x = i;           //  OK: assign to  enclose::x
	}
    };
};

inner* p = 0;                   //  error:  inner  not in scope

--- end example]

-2- Member functions and static data members of a nested class can be defined in a namespace scope enclosing the definition of their class. [Example:

class enclose {
public:
    class inner {
	static int x;
	void f(int i);
    };
};
int enclose::inner::x = 1;

void enclose::inner::f(int i) { /* ... */ }

--- end example]

-3- If class X is defined in a namespace scope, a nested class Y may be declared in class X and later defined in the definition of class X or be later defined in a namespace scope enclosing the definition of class X. [Example:

class E {
    class I1;                   //  forward declaration of nested class
    class I2;
    class I1 {};                //  definition of nested class
};
class E::I2 {};                 //  definition of nested class

--- end example]

-4- Like a member function, a friend function (class.friend) defined within a nested class is in the lexical scope of that class; it obeys the same rules for name binding as a static member function of that class (class.static) and has no special access rights to members of an enclosing class.

9.8 - Local class declarations [class.local]

-1- A class can be defined within a function definition; such a class is called a local class. The name of a local class is local to its enclosing scope. The local class is in the scope of the enclosing scope, and has the same access to names outside the function as does the enclosing function. Declarations in a local class can use only type names, static variables, extern variables and functions, and enumerators from the enclosing scope. [Example:

int x;
void f()
{
    static int s ;
    int x;
    extern int g();
    struct local {
	int g() { return x; }   //  error:  x  is auto
	int h() { return s; }   //  OK
	int k() { return ::x; } //  OK
	int l() { return g(); } //  OK
    };
    //  ...
}

local* p = 0;                   //  error:  local  not in scope

--- end example]

-2- An enclosing function has no special access to members of the local class; it obeys the usual access rules (clause class.access). Member functions of a local class shall be defined within their class definition, if they are defined at all.

-3- If class X is a local class a nested class Y may be declared in class X and later defined in the definition of class X or be later defined in the same scope as the definition of class X. A class nested within a local class is a local class.

-4- A local class shall not have static data members.

9.9 - Nested type names [class.nested.type]

-1- Type names obey exactly the same scope rules as other names. In particular, type names defined within a class definition cannot be used outside their class without qualification. [Example:

class X {
public:
    typedef int I;
    class Y { /* ... */ };
    I a;
};

I b;                            //  error
Y c;                            //  error
X::Y d;                         //  OK
X::I e;                         //  OK

--- end example]