- tmp/tmpgk3gq05q/{from.md → to.md} +171 -67
tmp/tmpgk3gq05q/{from.md → to.md}
RENAMED
|
@@ -13,11 +13,13 @@ scope* of a declaration. The scope of a declaration is the same as its
|
|
| 13 |
potential scope unless the potential scope contains another declaration
|
| 14 |
of the same name. In that case, the potential scope of the declaration
|
| 15 |
in the inner (contained) declarative region is excluded from the scope
|
| 16 |
of the declaration in the outer (containing) declarative region.
|
| 17 |
|
| 18 |
-
|
|
|
|
|
|
|
| 19 |
|
| 20 |
``` cpp
|
| 21 |
int j = 24;
|
| 22 |
int main() {
|
| 23 |
int i = j, j;
|
|
@@ -33,10 +35,12 @@ text between the `,` and the `}`. The declarative region of the second
|
|
| 33 |
declaration of `j` (the `j` immediately before the semicolon) includes
|
| 34 |
all the text between `{` and `}`, but its potential scope excludes the
|
| 35 |
declaration of `i`. The scope of the second declaration of `j` is the
|
| 36 |
same as its potential scope.
|
| 37 |
|
|
|
|
|
|
|
| 38 |
The names declared by a declaration are introduced into the scope in
|
| 39 |
which the declaration occurs, except that the presence of a `friend`
|
| 40 |
specifier ([[class.friend]]), certain uses of the
|
| 41 |
*elaborated-type-specifier* ([[dcl.type.elab]]), and
|
| 42 |
*using-directive*s ([[namespace.udir]]) alter this general behavior.
|
|
@@ -46,86 +50,110 @@ which specifies the same unqualified name,
|
|
| 46 |
|
| 47 |
- they shall all refer to the same entity, or all refer to functions and
|
| 48 |
function templates; or
|
| 49 |
- exactly one declaration shall declare a class name or enumeration name
|
| 50 |
that is not a typedef name and the other declarations shall all refer
|
| 51 |
-
to the same variable or enumerator, or all
|
| 52 |
-
function templates; in this case the class name
|
| 53 |
-
hidden ([[basic.scope.hiding]]).
|
| 54 |
-
name must be unique in its
|
| 55 |
-
|
|
|
|
| 56 |
|
| 57 |
-
These restrictions apply to the declarative region into
|
| 58 |
-
introduced, which is not necessarily the same as the
|
| 59 |
-
declaration occurs. In particular,
|
| 60 |
-
[[dcl.type.elab]]) and friend
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
introduce a (possibly not visible) name into an enclosing namespace;
|
| 62 |
-
these restrictions apply to
|
| 63 |
-
[[basic.link]]) may introduce a name into the declarative region where
|
| 64 |
-
the declaration appears and also introduce a (possibly not visible) name
|
| 65 |
-
into an enclosing namespace; these restrictions apply to both regions.
|
| 66 |
|
| 67 |
-
The name lookup rules are summarized in
|
|
|
|
| 68 |
|
| 69 |
### Point of declaration <a id="basic.scope.pdecl">[[basic.scope.pdecl]]</a>
|
| 70 |
|
| 71 |
The *point of declaration* for a name is immediately after its complete
|
| 72 |
declarator (Clause [[dcl.decl]]) and before its *initializer* (if any),
|
| 73 |
except as noted below.
|
| 74 |
|
|
|
|
|
|
|
| 75 |
``` cpp
|
| 76 |
unsigned char x = 12;
|
| 77 |
{ unsigned char x = x; }
|
| 78 |
```
|
| 79 |
|
| 80 |
Here the second `x` is initialized with its own (indeterminate) value.
|
| 81 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
a name from an outer scope remains visible up to the point of
|
| 83 |
declaration of the name that hides it.
|
| 84 |
|
|
|
|
|
|
|
| 85 |
``` cpp
|
| 86 |
const int i = 2;
|
| 87 |
{ int i[i]; }
|
| 88 |
```
|
| 89 |
|
| 90 |
declares a block-scope array of two integers.
|
| 91 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
The point of declaration for a class or class template first declared by
|
| 93 |
a *class-specifier* is immediately after the *identifier* or
|
| 94 |
*simple-template-id* (if any) in its *class-head* (Clause [[class]]).
|
| 95 |
The point of declaration for an enumeration is immediately after the
|
| 96 |
*identifier* (if any) in either its *enum-specifier* ([[dcl.enum]]) or
|
| 97 |
its first *opaque-enum-declaration* ([[dcl.enum]]), whichever comes
|
| 98 |
first. The point of declaration of an alias or alias template
|
| 99 |
immediately follows the *type-id* to which the alias refers.
|
| 100 |
|
| 101 |
-
The point of declaration of a *using-
|
| 102 |
-
constructor is immediately after the *using-
|
| 103 |
[[namespace.udecl]]).
|
| 104 |
|
| 105 |
The point of declaration for an enumerator is immediately after its
|
| 106 |
*enumerator-definition*.
|
| 107 |
|
|
|
|
|
|
|
| 108 |
``` cpp
|
| 109 |
const int x = 12;
|
| 110 |
{ enum { x = x }; }
|
| 111 |
```
|
| 112 |
|
| 113 |
Here, the enumerator `x` is initialized with the value of the constant
|
| 114 |
`x`, namely 12.
|
| 115 |
|
|
|
|
|
|
|
| 116 |
After the point of declaration of a class member, the member name can be
|
| 117 |
-
looked up in the scope of its class.
|
| 118 |
-
|
|
|
|
|
|
|
|
|
|
| 119 |
|
| 120 |
``` cpp
|
| 121 |
struct X {
|
| 122 |
enum E { z = 16 };
|
| 123 |
int b[X::z]; // OK
|
| 124 |
};
|
| 125 |
```
|
| 126 |
|
|
|
|
|
|
|
| 127 |
The point of declaration of a class first declared in an
|
| 128 |
*elaborated-type-specifier* is as follows:
|
| 129 |
|
| 130 |
- for a declaration of the form
|
| 131 |
``` bnf
|
|
@@ -142,14 +170,15 @@ The point of declaration of a class first declared in an
|
|
| 142 |
if the *elaborated-type-specifier* is used in the *decl-specifier-seq*
|
| 143 |
or *parameter-declaration-clause* of a function defined in namespace
|
| 144 |
scope, the *identifier* is declared as a *class-name* in the namespace
|
| 145 |
that contains the declaration; otherwise, except as a friend
|
| 146 |
declaration, the *identifier* is declared in the smallest namespace or
|
| 147 |
-
block scope that contains the declaration.
|
| 148 |
-
within templates.
|
| 149 |
-
|
| 150 |
-
*type-name*. See
|
|
|
|
| 151 |
|
| 152 |
The point of declaration for an *injected-class-name* (Clause
|
| 153 |
[[class]]) is immediately following the opening brace of the class
|
| 154 |
definition.
|
| 155 |
|
|
@@ -158,26 +187,32 @@ The point of declaration for a function-local predefined variable (
|
|
| 158 |
definition.
|
| 159 |
|
| 160 |
The point of declaration for a template parameter is immediately after
|
| 161 |
its complete *template-parameter*.
|
| 162 |
|
|
|
|
|
|
|
| 163 |
``` cpp
|
| 164 |
typedef unsigned char T;
|
| 165 |
template<class T
|
| 166 |
= T // lookup finds the typedef name of unsigned char
|
| 167 |
, T // lookup finds the template parameter
|
| 168 |
N = 0> struct A { };
|
| 169 |
```
|
| 170 |
|
| 171 |
-
|
| 172 |
-
the nearest enclosing namespace, but they do not introduce new names
|
| 173 |
-
into that namespace ([[namespace.memdef]]). Function declarations at
|
| 174 |
-
block scope and variable declarations with the `extern` specifier at
|
| 175 |
-
block scope refer to declarations that are members of an enclosing
|
| 176 |
-
namespace, but they do not introduce new names into that scope.
|
| 177 |
|
| 178 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 179 |
|
| 180 |
### Block scope <a id="basic.scope.block">[[basic.scope.block]]</a>
|
| 181 |
|
| 182 |
A name declared in a block ([[stmt.block]]) is local to that block; it
|
| 183 |
has *block scope*. Its potential scope begins at its point of
|
|
@@ -197,14 +232,14 @@ in the outermost block of any handler associated with a
|
|
| 197 |
|
| 198 |
The name declared in an *exception-declaration* is local to the
|
| 199 |
*handler* and shall not be redeclared in the outermost block of the
|
| 200 |
*handler*.
|
| 201 |
|
| 202 |
-
Names declared in the *
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
condition of that statement nor in the outermost block (or, for the `if`
|
| 207 |
statement, any of the outermost blocks) of the controlled statement;
|
| 208 |
see [[stmt.select]].
|
| 209 |
|
| 210 |
### Function prototype scope <a id="basic.scope.proto">[[basic.scope.proto]]</a>
|
|
@@ -221,34 +256,32 @@ in the function in which they are declared. Only labels have function
|
|
| 221 |
scope.
|
| 222 |
|
| 223 |
### Namespace scope <a id="basic.scope.namespace">[[basic.scope.namespace]]</a>
|
| 224 |
|
| 225 |
The declarative region of a *namespace-definition* is its
|
| 226 |
-
*namespace-body*.
|
| 227 |
-
*
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
namespace
|
| 234 |
-
scope
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
*using-directive* that follows the member’s point of declaration.
|
| 239 |
|
| 240 |
``` cpp
|
| 241 |
namespace N {
|
| 242 |
int i;
|
| 243 |
int g(int a) { return a; }
|
| 244 |
int j();
|
| 245 |
void q();
|
| 246 |
}
|
| 247 |
namespace { int l=1; }
|
| 248 |
-
// the potential scope of l is from its point of declaration
|
| 249 |
-
// to the end of the translation unit
|
| 250 |
|
| 251 |
namespace N {
|
| 252 |
int g(char a) { // overloads N::g(int)
|
| 253 |
return l+a; // l is from unnamed namespace
|
| 254 |
}
|
|
@@ -261,14 +294,16 @@ namespace N {
|
|
| 261 |
}
|
| 262 |
int q(); // error: different return type
|
| 263 |
}
|
| 264 |
```
|
| 265 |
|
|
|
|
|
|
|
| 266 |
A namespace member can also be referred to after the `::` scope
|
| 267 |
resolution operator ([[expr.prim]]) applied to the name of its
|
| 268 |
namespace or the name of a namespace which nominates the member’s
|
| 269 |
-
namespace in a *using-directive
|
| 270 |
|
| 271 |
The outermost declarative region of a translation unit is also a
|
| 272 |
namespace, called the *global namespace*. A name declared in the global
|
| 273 |
namespace has *global namespace scope* (also called *global scope*). The
|
| 274 |
potential scope of such a name begins at its point of declaration (
|
|
@@ -276,11 +311,60 @@ potential scope of such a name begins at its point of declaration (
|
|
| 276 |
is its declarative region. A name with global namespace scope is said to
|
| 277 |
be a *global name*.
|
| 278 |
|
| 279 |
### Class scope <a id="basic.scope.class">[[basic.scope.class]]</a>
|
| 280 |
|
| 281 |
-
The
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 282 |
|
| 283 |
The name of a class member shall only be used as follows:
|
| 284 |
|
| 285 |
- in the scope of its class (as described above) or a class derived
|
| 286 |
(Clause [[class.derived]]) from its class,
|
|
@@ -309,10 +393,12 @@ Only template parameter names belong to this declarative region; any
|
|
| 309 |
other kind of name introduced by the *declaration* of a
|
| 310 |
*template-declaration* is instead introduced into the same declarative
|
| 311 |
region where it would be introduced as a result of a non-template
|
| 312 |
declaration of the same name.
|
| 313 |
|
|
|
|
|
|
|
| 314 |
``` cpp
|
| 315 |
namespace N {
|
| 316 |
template<class T> struct A { }; // #1
|
| 317 |
template<class U> void f(U) { } // #2
|
| 318 |
struct B {
|
|
@@ -320,58 +406,76 @@ namespace N {
|
|
| 320 |
};
|
| 321 |
}
|
| 322 |
```
|
| 323 |
|
| 324 |
The declarative regions of `T`, `U` and `V` are the
|
| 325 |
-
*template-declaration*s on lines
|
| 326 |
the names `A`, `f`, `g` and `C` all belong to the same declarative
|
| 327 |
region — namely, the *namespace-body* of `N`. (`g` is still considered
|
| 328 |
to belong to this declarative region in spite of its being hidden during
|
| 329 |
qualified and unqualified name lookup.)
|
| 330 |
|
|
|
|
|
|
|
| 331 |
The potential scope of a template parameter name begins at its point of
|
| 332 |
declaration ([[basic.scope.pdecl]]) and ends at the end of its
|
| 333 |
-
declarative region.
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 337 |
|
| 338 |
``` cpp
|
| 339 |
-
template<class T, T* p, class U = T> class X {
|
| 340 |
template<class T> void f(T* p = new T);
|
| 341 |
```
|
| 342 |
|
| 343 |
This also implies that a *template-parameter* can be used in the
|
| 344 |
specification of base classes. For example,
|
| 345 |
|
| 346 |
``` cpp
|
| 347 |
-
template<class T> class X : public Array<T> {
|
| 348 |
-
template<class T> class Y : public T {
|
| 349 |
```
|
| 350 |
|
| 351 |
The use of a template parameter as a base class implies that a class
|
| 352 |
used as a template argument must be defined and not just declared when
|
| 353 |
the class template is instantiated.
|
| 354 |
|
|
|
|
|
|
|
| 355 |
The declarative region of the name of a template parameter is nested
|
| 356 |
-
within the immediately-enclosing declarative region.
|
| 357 |
-
|
| 358 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 359 |
|
| 360 |
``` cpp
|
| 361 |
typedef int N;
|
| 362 |
template<N X, typename N, template<N Y> class T> struct A;
|
| 363 |
```
|
| 364 |
|
| 365 |
Here, `X` is a non-type template parameter of type `int` and `Y` is a
|
| 366 |
non-type template parameter of the same type as the second template
|
| 367 |
parameter of `A`.
|
| 368 |
|
| 369 |
-
|
| 370 |
-
|
| 371 |
-
|
| 372 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 373 |
|
| 374 |
### Name hiding <a id="basic.scope.hiding">[[basic.scope.hiding]]</a>
|
| 375 |
|
| 376 |
A name can be hidden by an explicit declaration of that same name in a
|
| 377 |
nested declarative region or derived class ([[class.member.lookup]]).
|
|
@@ -391,9 +495,9 @@ class (Clause [[class.derived]]) hides the declaration of a member of a
|
|
| 391 |
base class of the same name; see [[class.member.lookup]].
|
| 392 |
|
| 393 |
During the lookup of a name qualified by a namespace name, declarations
|
| 394 |
that would otherwise be made visible by a *using-directive* can be
|
| 395 |
hidden by declarations with the same name in the namespace containing
|
| 396 |
-
the *using-directive
|
| 397 |
|
| 398 |
If a name is in scope and is not hidden it is said to be *visible*.
|
| 399 |
|
|
|
|
| 13 |
potential scope unless the potential scope contains another declaration
|
| 14 |
of the same name. In that case, the potential scope of the declaration
|
| 15 |
in the inner (contained) declarative region is excluded from the scope
|
| 16 |
of the declaration in the outer (containing) declarative region.
|
| 17 |
|
| 18 |
+
[*Example 1*:
|
| 19 |
+
|
| 20 |
+
In
|
| 21 |
|
| 22 |
``` cpp
|
| 23 |
int j = 24;
|
| 24 |
int main() {
|
| 25 |
int i = j, j;
|
|
|
|
| 35 |
declaration of `j` (the `j` immediately before the semicolon) includes
|
| 36 |
all the text between `{` and `}`, but its potential scope excludes the
|
| 37 |
declaration of `i`. The scope of the second declaration of `j` is the
|
| 38 |
same as its potential scope.
|
| 39 |
|
| 40 |
+
— *end example*]
|
| 41 |
+
|
| 42 |
The names declared by a declaration are introduced into the scope in
|
| 43 |
which the declaration occurs, except that the presence of a `friend`
|
| 44 |
specifier ([[class.friend]]), certain uses of the
|
| 45 |
*elaborated-type-specifier* ([[dcl.type.elab]]), and
|
| 46 |
*using-directive*s ([[namespace.udir]]) alter this general behavior.
|
|
|
|
| 50 |
|
| 51 |
- they shall all refer to the same entity, or all refer to functions and
|
| 52 |
function templates; or
|
| 53 |
- exactly one declaration shall declare a class name or enumeration name
|
| 54 |
that is not a typedef name and the other declarations shall all refer
|
| 55 |
+
to the same variable, non-static data member, or enumerator, or all
|
| 56 |
+
refer to functions and function templates; in this case the class name
|
| 57 |
+
or enumeration name is hidden ([[basic.scope.hiding]]). \[*Note 1*: A
|
| 58 |
+
namespace name or a class template name must be unique in its
|
| 59 |
+
declarative region ([[namespace.alias]], Clause
|
| 60 |
+
[[temp]]). — *end note*]
|
| 61 |
|
| 62 |
+
[*Note 2*: These restrictions apply to the declarative region into
|
| 63 |
+
which a name is introduced, which is not necessarily the same as the
|
| 64 |
+
region in which the declaration occurs. In particular,
|
| 65 |
+
*elaborated-type-specifier*s ([[dcl.type.elab]]) and friend
|
| 66 |
+
declarations ([[class.friend]]) may introduce a (possibly not visible)
|
| 67 |
+
name into an enclosing namespace; these restrictions apply to that
|
| 68 |
+
region. Local extern declarations ([[basic.link]]) may introduce a name
|
| 69 |
+
into the declarative region where the declaration appears and also
|
| 70 |
introduce a (possibly not visible) name into an enclosing namespace;
|
| 71 |
+
these restrictions apply to both regions. — *end note*]
|
|
|
|
|
|
|
|
|
|
| 72 |
|
| 73 |
+
[*Note 3*: The name lookup rules are summarized in
|
| 74 |
+
[[basic.lookup]]. — *end note*]
|
| 75 |
|
| 76 |
### Point of declaration <a id="basic.scope.pdecl">[[basic.scope.pdecl]]</a>
|
| 77 |
|
| 78 |
The *point of declaration* for a name is immediately after its complete
|
| 79 |
declarator (Clause [[dcl.decl]]) and before its *initializer* (if any),
|
| 80 |
except as noted below.
|
| 81 |
|
| 82 |
+
[*Example 1*:
|
| 83 |
+
|
| 84 |
``` cpp
|
| 85 |
unsigned char x = 12;
|
| 86 |
{ unsigned char x = x; }
|
| 87 |
```
|
| 88 |
|
| 89 |
Here the second `x` is initialized with its own (indeterminate) value.
|
| 90 |
|
| 91 |
+
— *end example*]
|
| 92 |
+
|
| 93 |
+
[*Note 1*:
|
| 94 |
+
|
| 95 |
a name from an outer scope remains visible up to the point of
|
| 96 |
declaration of the name that hides it.
|
| 97 |
|
| 98 |
+
[*Example 2*:
|
| 99 |
+
|
| 100 |
``` cpp
|
| 101 |
const int i = 2;
|
| 102 |
{ int i[i]; }
|
| 103 |
```
|
| 104 |
|
| 105 |
declares a block-scope array of two integers.
|
| 106 |
|
| 107 |
+
— *end example*]
|
| 108 |
+
|
| 109 |
+
— *end note*]
|
| 110 |
+
|
| 111 |
The point of declaration for a class or class template first declared by
|
| 112 |
a *class-specifier* is immediately after the *identifier* or
|
| 113 |
*simple-template-id* (if any) in its *class-head* (Clause [[class]]).
|
| 114 |
The point of declaration for an enumeration is immediately after the
|
| 115 |
*identifier* (if any) in either its *enum-specifier* ([[dcl.enum]]) or
|
| 116 |
its first *opaque-enum-declaration* ([[dcl.enum]]), whichever comes
|
| 117 |
first. The point of declaration of an alias or alias template
|
| 118 |
immediately follows the *type-id* to which the alias refers.
|
| 119 |
|
| 120 |
+
The point of declaration of a *using-declarator* that does not name a
|
| 121 |
+
constructor is immediately after the *using-declarator* (
|
| 122 |
[[namespace.udecl]]).
|
| 123 |
|
| 124 |
The point of declaration for an enumerator is immediately after its
|
| 125 |
*enumerator-definition*.
|
| 126 |
|
| 127 |
+
[*Example 3*:
|
| 128 |
+
|
| 129 |
``` cpp
|
| 130 |
const int x = 12;
|
| 131 |
{ enum { x = x }; }
|
| 132 |
```
|
| 133 |
|
| 134 |
Here, the enumerator `x` is initialized with the value of the constant
|
| 135 |
`x`, namely 12.
|
| 136 |
|
| 137 |
+
— *end example*]
|
| 138 |
+
|
| 139 |
After the point of declaration of a class member, the member name can be
|
| 140 |
+
looked up in the scope of its class.
|
| 141 |
+
|
| 142 |
+
[*Note 2*:
|
| 143 |
+
|
| 144 |
+
This is true even if the class is an incomplete class. For example,
|
| 145 |
|
| 146 |
``` cpp
|
| 147 |
struct X {
|
| 148 |
enum E { z = 16 };
|
| 149 |
int b[X::z]; // OK
|
| 150 |
};
|
| 151 |
```
|
| 152 |
|
| 153 |
+
— *end note*]
|
| 154 |
+
|
| 155 |
The point of declaration of a class first declared in an
|
| 156 |
*elaborated-type-specifier* is as follows:
|
| 157 |
|
| 158 |
- for a declaration of the form
|
| 159 |
``` bnf
|
|
|
|
| 170 |
if the *elaborated-type-specifier* is used in the *decl-specifier-seq*
|
| 171 |
or *parameter-declaration-clause* of a function defined in namespace
|
| 172 |
scope, the *identifier* is declared as a *class-name* in the namespace
|
| 173 |
that contains the declaration; otherwise, except as a friend
|
| 174 |
declaration, the *identifier* is declared in the smallest namespace or
|
| 175 |
+
block scope that contains the declaration.
|
| 176 |
+
\[*Note 3*: These rules also apply within templates. — *end note*]
|
| 177 |
+
\[*Note 4*: Other forms of *elaborated-type-specifier* do not declare
|
| 178 |
+
a new name, and therefore must refer to an existing *type-name*. See
|
| 179 |
+
[[basic.lookup.elab]] and [[dcl.type.elab]]. — *end note*]
|
| 180 |
|
| 181 |
The point of declaration for an *injected-class-name* (Clause
|
| 182 |
[[class]]) is immediately following the opening brace of the class
|
| 183 |
definition.
|
| 184 |
|
|
|
|
| 187 |
definition.
|
| 188 |
|
| 189 |
The point of declaration for a template parameter is immediately after
|
| 190 |
its complete *template-parameter*.
|
| 191 |
|
| 192 |
+
[*Example 4*:
|
| 193 |
+
|
| 194 |
``` cpp
|
| 195 |
typedef unsigned char T;
|
| 196 |
template<class T
|
| 197 |
= T // lookup finds the typedef name of unsigned char
|
| 198 |
, T // lookup finds the template parameter
|
| 199 |
N = 0> struct A { };
|
| 200 |
```
|
| 201 |
|
| 202 |
+
— *end example*]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 203 |
|
| 204 |
+
[*Note 5*: Friend declarations refer to functions or classes that are
|
| 205 |
+
members of the nearest enclosing namespace, but they do not introduce
|
| 206 |
+
new names into that namespace ([[namespace.memdef]]). Function
|
| 207 |
+
declarations at block scope and variable declarations with the `extern`
|
| 208 |
+
specifier at block scope refer to declarations that are members of an
|
| 209 |
+
enclosing namespace, but they do not introduce new names into that
|
| 210 |
+
scope. — *end note*]
|
| 211 |
+
|
| 212 |
+
[*Note 6*: For point of instantiation of a template, see
|
| 213 |
+
[[temp.point]]. — *end note*]
|
| 214 |
|
| 215 |
### Block scope <a id="basic.scope.block">[[basic.scope.block]]</a>
|
| 216 |
|
| 217 |
A name declared in a block ([[stmt.block]]) is local to that block; it
|
| 218 |
has *block scope*. Its potential scope begins at its point of
|
|
|
|
| 232 |
|
| 233 |
The name declared in an *exception-declaration* is local to the
|
| 234 |
*handler* and shall not be redeclared in the outermost block of the
|
| 235 |
*handler*.
|
| 236 |
|
| 237 |
+
Names declared in the *init-statement*, the *for-range-declaration*, and
|
| 238 |
+
in the *condition* of `if`, `while`, `for`, and `switch` statements are
|
| 239 |
+
local to the `if`, `while`, `for`, or `switch` statement (including the
|
| 240 |
+
controlled statement), and shall not be redeclared in a subsequent
|
| 241 |
condition of that statement nor in the outermost block (or, for the `if`
|
| 242 |
statement, any of the outermost blocks) of the controlled statement;
|
| 243 |
see [[stmt.select]].
|
| 244 |
|
| 245 |
### Function prototype scope <a id="basic.scope.proto">[[basic.scope.proto]]</a>
|
|
|
|
| 256 |
scope.
|
| 257 |
|
| 258 |
### Namespace scope <a id="basic.scope.namespace">[[basic.scope.namespace]]</a>
|
| 259 |
|
| 260 |
The declarative region of a *namespace-definition* is its
|
| 261 |
+
*namespace-body*. Entities declared in a *namespace-body* are said to be
|
| 262 |
+
*members* of the namespace, and names introduced by these declarations
|
| 263 |
+
into the declarative region of the namespace are said to be *member
|
| 264 |
+
names* of the namespace. A namespace member name has namespace scope.
|
| 265 |
+
Its potential scope includes its namespace from the name’s point of
|
| 266 |
+
declaration ([[basic.scope.pdecl]]) onwards; and for each
|
| 267 |
+
*using-directive* ([[namespace.udir]]) that nominates the member’s
|
| 268 |
+
namespace, the member’s potential scope includes that portion of the
|
| 269 |
+
potential scope of the *using-directive* that follows the member’s point
|
| 270 |
+
of declaration.
|
| 271 |
+
|
| 272 |
+
[*Example 1*:
|
|
|
|
| 273 |
|
| 274 |
``` cpp
|
| 275 |
namespace N {
|
| 276 |
int i;
|
| 277 |
int g(int a) { return a; }
|
| 278 |
int j();
|
| 279 |
void q();
|
| 280 |
}
|
| 281 |
namespace { int l=1; }
|
| 282 |
+
// the potential scope of l is from its point of declaration to the end of the translation unit
|
|
|
|
| 283 |
|
| 284 |
namespace N {
|
| 285 |
int g(char a) { // overloads N::g(int)
|
| 286 |
return l+a; // l is from unnamed namespace
|
| 287 |
}
|
|
|
|
| 294 |
}
|
| 295 |
int q(); // error: different return type
|
| 296 |
}
|
| 297 |
```
|
| 298 |
|
| 299 |
+
— *end example*]
|
| 300 |
+
|
| 301 |
A namespace member can also be referred to after the `::` scope
|
| 302 |
resolution operator ([[expr.prim]]) applied to the name of its
|
| 303 |
namespace or the name of a namespace which nominates the member’s
|
| 304 |
+
namespace in a *using-directive*; see [[namespace.qual]].
|
| 305 |
|
| 306 |
The outermost declarative region of a translation unit is also a
|
| 307 |
namespace, called the *global namespace*. A name declared in the global
|
| 308 |
namespace has *global namespace scope* (also called *global scope*). The
|
| 309 |
potential scope of such a name begins at its point of declaration (
|
|
|
|
| 311 |
is its declarative region. A name with global namespace scope is said to
|
| 312 |
be a *global name*.
|
| 313 |
|
| 314 |
### Class scope <a id="basic.scope.class">[[basic.scope.class]]</a>
|
| 315 |
|
| 316 |
+
The potential scope of a name declared in a class consists not only of
|
| 317 |
+
the declarative region following the name’s point of declaration, but
|
| 318 |
+
also of all function bodies, default arguments, *noexcept-specifier*s,
|
| 319 |
+
and *brace-or-equal-initializer*s of non-static data members in that
|
| 320 |
+
class (including such things in nested classes).
|
| 321 |
+
|
| 322 |
+
A name `N` used in a class `S` shall refer to the same declaration in
|
| 323 |
+
its context and when re-evaluated in the completed scope of `S`. No
|
| 324 |
+
diagnostic is required for a violation of this rule.
|
| 325 |
+
|
| 326 |
+
A name declared within a member function hides a declaration of the same
|
| 327 |
+
name whose scope extends to or past the end of the member function’s
|
| 328 |
+
class.
|
| 329 |
+
|
| 330 |
+
The potential scope of a declaration that extends to or past the end of
|
| 331 |
+
a class definition also extends to the regions defined by its member
|
| 332 |
+
definitions, even if the members are defined lexically outside the class
|
| 333 |
+
(this includes static data member definitions, nested class definitions,
|
| 334 |
+
and member function definitions, including the member function body and
|
| 335 |
+
any portion of the declarator part of such definitions which follows the
|
| 336 |
+
*declarator-id*, including a *parameter-declaration-clause* and any
|
| 337 |
+
default arguments ([[dcl.fct.default]])).
|
| 338 |
+
|
| 339 |
+
[*Example 1*:
|
| 340 |
+
|
| 341 |
+
``` cpp
|
| 342 |
+
typedef int c;
|
| 343 |
+
enum { i = 1 };
|
| 344 |
+
|
| 345 |
+
class X {
|
| 346 |
+
char v[i]; // error: i refers to ::i but when reevaluated is X::i
|
| 347 |
+
int f() { return sizeof(c); } // OK: X::c
|
| 348 |
+
char c;
|
| 349 |
+
enum { i = 2 };
|
| 350 |
+
};
|
| 351 |
+
|
| 352 |
+
typedef char* T;
|
| 353 |
+
struct Y {
|
| 354 |
+
T a; // error: T refers to ::T but when reevaluated is Y::T
|
| 355 |
+
typedef long T;
|
| 356 |
+
T b;
|
| 357 |
+
};
|
| 358 |
+
|
| 359 |
+
typedef int I;
|
| 360 |
+
class D {
|
| 361 |
+
typedef I I; // error, even though no reordering involved
|
| 362 |
+
};
|
| 363 |
+
```
|
| 364 |
+
|
| 365 |
+
— *end example*]
|
| 366 |
|
| 367 |
The name of a class member shall only be used as follows:
|
| 368 |
|
| 369 |
- in the scope of its class (as described above) or a class derived
|
| 370 |
(Clause [[class.derived]]) from its class,
|
|
|
|
| 393 |
other kind of name introduced by the *declaration* of a
|
| 394 |
*template-declaration* is instead introduced into the same declarative
|
| 395 |
region where it would be introduced as a result of a non-template
|
| 396 |
declaration of the same name.
|
| 397 |
|
| 398 |
+
[*Example 1*:
|
| 399 |
+
|
| 400 |
``` cpp
|
| 401 |
namespace N {
|
| 402 |
template<class T> struct A { }; // #1
|
| 403 |
template<class U> void f(U) { } // #2
|
| 404 |
struct B {
|
|
|
|
| 406 |
};
|
| 407 |
}
|
| 408 |
```
|
| 409 |
|
| 410 |
The declarative regions of `T`, `U` and `V` are the
|
| 411 |
+
*template-declaration*s on lines \#1, \#2, and \#3, respectively. But
|
| 412 |
the names `A`, `f`, `g` and `C` all belong to the same declarative
|
| 413 |
region — namely, the *namespace-body* of `N`. (`g` is still considered
|
| 414 |
to belong to this declarative region in spite of its being hidden during
|
| 415 |
qualified and unqualified name lookup.)
|
| 416 |
|
| 417 |
+
— *end example*]
|
| 418 |
+
|
| 419 |
The potential scope of a template parameter name begins at its point of
|
| 420 |
declaration ([[basic.scope.pdecl]]) and ends at the end of its
|
| 421 |
+
declarative region.
|
| 422 |
+
|
| 423 |
+
[*Note 1*:
|
| 424 |
+
|
| 425 |
+
This implies that a *template-parameter* can be used in the declaration
|
| 426 |
+
of subsequent *template-parameter*s and their default arguments but
|
| 427 |
+
cannot be used in preceding *template-parameter*s or their default
|
| 428 |
+
arguments. For example,
|
| 429 |
|
| 430 |
``` cpp
|
| 431 |
+
template<class T, T* p, class U = T> class X { ... };
|
| 432 |
template<class T> void f(T* p = new T);
|
| 433 |
```
|
| 434 |
|
| 435 |
This also implies that a *template-parameter* can be used in the
|
| 436 |
specification of base classes. For example,
|
| 437 |
|
| 438 |
``` cpp
|
| 439 |
+
template<class T> class X : public Array<T> { ... };
|
| 440 |
+
template<class T> class Y : public T { ... };
|
| 441 |
```
|
| 442 |
|
| 443 |
The use of a template parameter as a base class implies that a class
|
| 444 |
used as a template argument must be defined and not just declared when
|
| 445 |
the class template is instantiated.
|
| 446 |
|
| 447 |
+
— *end note*]
|
| 448 |
+
|
| 449 |
The declarative region of the name of a template parameter is nested
|
| 450 |
+
within the immediately-enclosing declarative region.
|
| 451 |
+
|
| 452 |
+
[*Note 2*:
|
| 453 |
+
|
| 454 |
+
As a result, a *template-parameter* hides any entity with the same name
|
| 455 |
+
in an enclosing scope ([[basic.scope.hiding]]).
|
| 456 |
+
|
| 457 |
+
[*Example 2*:
|
| 458 |
|
| 459 |
``` cpp
|
| 460 |
typedef int N;
|
| 461 |
template<N X, typename N, template<N Y> class T> struct A;
|
| 462 |
```
|
| 463 |
|
| 464 |
Here, `X` is a non-type template parameter of type `int` and `Y` is a
|
| 465 |
non-type template parameter of the same type as the second template
|
| 466 |
parameter of `A`.
|
| 467 |
|
| 468 |
+
— *end example*]
|
| 469 |
+
|
| 470 |
+
— *end note*]
|
| 471 |
+
|
| 472 |
+
[*Note 3*: Because the name of a template parameter cannot be
|
| 473 |
+
redeclared within its potential scope ([[temp.local]]), a template
|
| 474 |
+
parameter’s scope is often its potential scope. However, it is still
|
| 475 |
+
possible for a template parameter name to be hidden; see
|
| 476 |
+
[[temp.local]]. — *end note*]
|
| 477 |
|
| 478 |
### Name hiding <a id="basic.scope.hiding">[[basic.scope.hiding]]</a>
|
| 479 |
|
| 480 |
A name can be hidden by an explicit declaration of that same name in a
|
| 481 |
nested declarative region or derived class ([[class.member.lookup]]).
|
|
|
|
| 495 |
base class of the same name; see [[class.member.lookup]].
|
| 496 |
|
| 497 |
During the lookup of a name qualified by a namespace name, declarations
|
| 498 |
that would otherwise be made visible by a *using-directive* can be
|
| 499 |
hidden by declarations with the same name in the namespace containing
|
| 500 |
+
the *using-directive*; see [[namespace.qual]].
|
| 501 |
|
| 502 |
If a name is in scope and is not hidden it is said to be *visible*.
|
| 503 |
|