9 Classes [class]

9.8 Local 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 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 shall not odr-use ([basic.def.odr]) 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; }       // error: odr-use of automatic variable x
    int h() { return s; }       // OK
    int k() { return ::x; }     // OK
    int l() { return q(); }     // OK
    int m() { return N; }       // OK: not an odr-use
    int *n() { return &N; }     // error: odr-use of automatic variable N

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

 — end example ]

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.

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.

A local class shall not have static data members.