57 lines
2.2 KiB
Markdown
57 lines
2.2 KiB
Markdown
[class.local]
|
||
|
||
# 11 Classes [[class]](./#class)
|
||
|
||
## 11.6 Local class declarations [class.local]
|
||
|
||
[1](#1)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/classes.tex#L3443)
|
||
|
||
A class can be declared within a function definition; such a class is
|
||
called a [*local class*](#def:class,local "11.6 Local class declarations [class.local]")[.](#1.sentence-1)
|
||
|
||
[*Note [1](#note-1)*:
|
||
|
||
A declaration in a local class
|
||
cannot odr-use ([[basic.def.odr]](basic.def.odr#term.odr.use "6.3 One-definition rule"))
|
||
a local entity
|
||
from an
|
||
enclosing scope[.](#1.sentence-2)
|
||
|
||
â *end note*]
|
||
|
||
[*Example [1](#example-1)*: int x;void f() {static int s; int x; const int N = 5; extern int q(); int arr[2]; auto [y, z] = arr; struct local {int g() { return x; } // error: odr-use of non-odr-usable variable xint h() { return s; } // OKint k() { return ::x; } // OKint l() { return q(); } // OKint m() { return N; } // OK, not an odr-useint* n() { return &N; } // error: odr-use of non-odr-usable variable Nint p() { return y; } // error: odr-use of non-odr-usable structured binding y};} local* p = 0; // error: local not found â *end example*]
|
||
|
||
[2](#2)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/classes.tex#L3479)
|
||
|
||
An enclosing function has no special access to members of the local
|
||
class; it obeys the usual access rules ([[class.access]](class.access "11.8 Member access control"))[.](#2.sentence-1)
|
||
|
||
Member functions of a local class shall be defined within their class
|
||
definition, if they are defined at all[.](#2.sentence-2)
|
||
|
||
[3](#3)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/classes.tex#L3486)
|
||
|
||
A class nested within
|
||
a local class is a local class[.](#3.sentence-1)
|
||
|
||
A member of a local class X shall be
|
||
declared only in the definition of X or,
|
||
if the member is a nested class,
|
||
in the nearest enclosing block scope of X[.](#3.sentence-2)
|
||
|
||
[4](#4)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/classes.tex#L3496)
|
||
|
||
[*Note [2](#note-2)*:
|
||
|
||
A local class cannot have static data members ([[class.static.data]](class.static.data "11.4.9.3 Static data members"))[.](#4.sentence-1)
|
||
|
||
â *end note*]
|