- tmp/tmpkudtzdeb/{from.md → to.md} +162 -96
tmp/tmpkudtzdeb/{from.md → to.md}
RENAMED
|
@@ -2,11 +2,11 @@
|
|
| 2 |
|
| 3 |
### Declarative regions and scopes <a id="basic.scope.declarative">[[basic.scope.declarative]]</a>
|
| 4 |
|
| 5 |
Every name is introduced in some portion of program text called a
|
| 6 |
*declarative region*, which is the largest part of the program in which
|
| 7 |
-
that name is
|
| 8 |
unqualified name to refer to the same entity. In general, each
|
| 9 |
particular name is valid only within some possibly discontiguous portion
|
| 10 |
of program text called its *scope*. To determine the scope of a
|
| 11 |
declaration, it is sometimes convenient to refer to the *potential
|
| 12 |
scope* of a declaration. The scope of a declaration is the same as its
|
|
@@ -39,62 +39,68 @@ 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
|
| 45 |
-
*elaborated-type-specifier*
|
| 46 |
-
|
| 47 |
|
| 48 |
Given a set of declarations in a single declarative region, each of
|
| 49 |
which specifies the same unqualified name,
|
| 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
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 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
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|
| 80 |
-
|
| 81 |
|
| 82 |
[*Example 1*:
|
| 83 |
|
| 84 |
``` cpp
|
| 85 |
unsigned char x = 12;
|
| 86 |
{ unsigned char x = x; }
|
| 87 |
```
|
| 88 |
|
| 89 |
-
Here the second `x`
|
|
|
|
|
|
|
| 90 |
|
| 91 |
— *end example*]
|
| 92 |
|
| 93 |
[*Note 1*:
|
| 94 |
|
| 95 |
-
|
| 96 |
declaration of the name that hides it.
|
| 97 |
|
| 98 |
[*Example 2*:
|
| 99 |
|
| 100 |
``` cpp
|
|
@@ -108,20 +114,20 @@ declares a block-scope array of two integers.
|
|
| 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*
|
| 114 |
-
|
| 115 |
-
*identifier* (if any) in either its *enum-specifier*
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 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*:
|
|
@@ -176,17 +182,24 @@ The point of declaration of a class first declared in an
|
|
| 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
|
| 182 |
-
|
| 183 |
-
definition.
|
| 184 |
|
| 185 |
-
The point of declaration for a function-local predefined variable
|
| 186 |
-
[[dcl.fct.def]]
|
| 187 |
-
definition.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 188 |
|
| 189 |
The point of declaration for a template parameter is immediately after
|
| 190 |
its complete *template-parameter*.
|
| 191 |
|
| 192 |
[*Example 4*:
|
|
@@ -201,72 +214,81 @@ template<class T
|
|
| 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
|
| 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
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
The potential scope of a function parameter name (including one
|
| 223 |
-
appearing in a *lambda-declarator*) or of a function-local predefined
|
| 224 |
-
variable in a function definition ([[dcl.fct.def]]) begins at its point
|
| 225 |
-
of declaration. If the function has a *function-try-block* the potential
|
| 226 |
-
scope of a parameter or of a function-local predefined variable ends at
|
| 227 |
-
the end of the last associated handler, otherwise it ends at the end of
|
| 228 |
-
the outermost block of the function definition. A parameter name shall
|
| 229 |
-
not be redeclared in the outermost block of the function definition nor
|
| 230 |
-
in the outermost block of any handler associated with a
|
| 231 |
-
*function-try-block*.
|
| 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 |
-
|
| 246 |
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 251 |
|
| 252 |
### Function scope <a id="basic.funscope">[[basic.funscope]]</a>
|
| 253 |
|
| 254 |
-
Labels
|
| 255 |
-
|
| 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
|
| 267 |
-
*using-directive*
|
| 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*:
|
|
@@ -296,47 +318,90 @@ namespace N {
|
|
| 296 |
}
|
| 297 |
```
|
| 298 |
|
| 299 |
— *end example*]
|
| 300 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 301 |
A namespace member can also be referred to after the `::` scope
|
| 302 |
-
resolution operator
|
| 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
|
| 310 |
-
[[basic.scope.pdecl]]
|
| 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
|
| 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
|
| 331 |
-
a class definition also extends to the regions defined by its
|
| 332 |
-
definitions, even if the members are defined lexically outside
|
| 333 |
-
(this includes static data member definitions, nested class
|
| 334 |
-
and member function definitions, including the member
|
| 335 |
-
any portion of the declarator part of such definitions
|
| 336 |
-
*declarator-id*, including a
|
| 337 |
-
default arguments
|
|
|
|
| 338 |
|
| 339 |
[*Example 1*:
|
| 340 |
|
| 341 |
``` cpp
|
| 342 |
typedef int c;
|
|
@@ -365,21 +430,21 @@ class D {
|
|
| 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 |
-
|
| 371 |
- after the `.` operator applied to an expression of the type of its
|
| 372 |
-
class
|
| 373 |
-
- after the `->` operator applied to a pointer to an object of its
|
| 374 |
-
|
| 375 |
-
- after the `::` scope resolution operator
|
| 376 |
-
the name of its class or a class derived from its class.
|
| 377 |
|
| 378 |
### Enumeration scope <a id="basic.scope.enum">[[basic.scope.enum]]</a>
|
| 379 |
|
| 380 |
-
The name of a scoped enumerator
|
| 381 |
Its potential scope begins at its point of declaration and terminates at
|
| 382 |
the end of the *enum-specifier*.
|
| 383 |
|
| 384 |
### Template parameter scope <a id="basic.scope.temp">[[basic.scope.temp]]</a>
|
| 385 |
|
|
@@ -415,12 +480,12 @@ 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
|
| 421 |
-
|
| 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
|
|
@@ -450,11 +515,11 @@ 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
|
| 456 |
|
| 457 |
[*Example 2*:
|
| 458 |
|
| 459 |
``` cpp
|
| 460 |
typedef int N;
|
|
@@ -468,33 +533,34 @@ parameter of `A`.
|
|
| 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
|
| 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
|
| 481 |
-
|
|
|
|
| 482 |
|
| 483 |
-
|
| 484 |
-
|
| 485 |
-
|
| 486 |
-
|
| 487 |
-
|
| 488 |
-
|
| 489 |
-
|
| 490 |
|
| 491 |
In a member function definition, the declaration of a name at block
|
| 492 |
scope hides the declaration of a member of the class with the same name;
|
| 493 |
see [[basic.scope.class]]. The declaration of a member in a derived
|
| 494 |
-
class
|
| 495 |
-
|
| 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]].
|
|
|
|
| 2 |
|
| 3 |
### Declarative regions and scopes <a id="basic.scope.declarative">[[basic.scope.declarative]]</a>
|
| 4 |
|
| 5 |
Every name is introduced in some portion of program text called a
|
| 6 |
*declarative region*, which is the largest part of the program in which
|
| 7 |
+
that name is valid, that is, in which that name may be used as an
|
| 8 |
unqualified name to refer to the same entity. In general, each
|
| 9 |
particular name is valid only within some possibly discontiguous portion
|
| 10 |
of program text called its *scope*. To determine the scope of a
|
| 11 |
declaration, it is sometimes convenient to refer to the *potential
|
| 12 |
scope* of a declaration. The scope of a declaration is the same as its
|
|
|
|
| 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 *using-directive*s
|
| 46 |
+
[[namespace.udir]] alter this general behavior.
|
| 47 |
|
| 48 |
Given a set of declarations in a single declarative region, each of
|
| 49 |
which specifies the same unqualified name,
|
| 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 |
+
structured binding [[dcl.struct.bind]], namespace name
|
| 59 |
+
[[basic.namespace]], or class template name [[temp.pre]] must be
|
| 60 |
+
unique in its declarative region. — *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 declarations
|
| 66 |
+
[[class.friend]] may introduce a (possibly not visible) name into an
|
| 67 |
+
enclosing namespace; these restrictions apply to that region. Local
|
| 68 |
+
extern declarations [[basic.link]] may introduce a name into the
|
| 69 |
+
declarative region where the declaration appears and also introduce a
|
| 70 |
+
(possibly not visible) name into an enclosing namespace; these
|
| 71 |
+
restrictions apply to both regions. — *end note*]
|
| 72 |
+
|
| 73 |
+
For a given declarative region *R* and a point *P* outside *R*, the set
|
| 74 |
+
of *intervening* declarative regions between *P* and *R* comprises all
|
| 75 |
+
declarative regions that are or enclose *R* and do not enclose *P*.
|
| 76 |
|
| 77 |
[*Note 3*: The name lookup rules are summarized in
|
| 78 |
[[basic.lookup]]. — *end note*]
|
| 79 |
|
| 80 |
### Point of declaration <a id="basic.scope.pdecl">[[basic.scope.pdecl]]</a>
|
| 81 |
|
| 82 |
The *point of declaration* for a name is immediately after its complete
|
| 83 |
+
declarator [[dcl.decl]] and before its *initializer* (if any), except as
|
| 84 |
+
noted below.
|
| 85 |
|
| 86 |
[*Example 1*:
|
| 87 |
|
| 88 |
``` cpp
|
| 89 |
unsigned char x = 12;
|
| 90 |
{ unsigned char x = x; }
|
| 91 |
```
|
| 92 |
|
| 93 |
+
Here, the initialization of the second `x` has undefined behavior,
|
| 94 |
+
because the initializer accesses the second `x` outside its lifetime
|
| 95 |
+
[[basic.life]].
|
| 96 |
|
| 97 |
— *end example*]
|
| 98 |
|
| 99 |
[*Note 1*:
|
| 100 |
|
| 101 |
+
A name from an outer scope remains visible up to the point of
|
| 102 |
declaration of the name that hides it.
|
| 103 |
|
| 104 |
[*Example 2*:
|
| 105 |
|
| 106 |
``` cpp
|
|
|
|
| 114 |
|
| 115 |
— *end note*]
|
| 116 |
|
| 117 |
The point of declaration for a class or class template first declared by
|
| 118 |
a *class-specifier* is immediately after the *identifier* or
|
| 119 |
+
*simple-template-id* (if any) in its *class-head* [[class.pre]]. The
|
| 120 |
+
point of declaration for an enumeration is immediately after the
|
| 121 |
+
*identifier* (if any) in either its *enum-specifier* [[dcl.enum]] or its
|
| 122 |
+
first *opaque-enum-declaration* [[dcl.enum]], whichever comes first. The
|
| 123 |
+
point of declaration of an alias or alias template immediately follows
|
| 124 |
+
the *defining-type-id* to which the alias refers.
|
| 125 |
|
| 126 |
The point of declaration of a *using-declarator* that does not name a
|
| 127 |
+
constructor is immediately after the *using-declarator*
|
| 128 |
+
[[namespace.udecl]].
|
| 129 |
|
| 130 |
The point of declaration for an enumerator is immediately after its
|
| 131 |
*enumerator-definition*.
|
| 132 |
|
| 133 |
[*Example 3*:
|
|
|
|
| 182 |
\[*Note 3*: These rules also apply within templates. — *end note*]
|
| 183 |
\[*Note 4*: Other forms of *elaborated-type-specifier* do not declare
|
| 184 |
a new name, and therefore must refer to an existing *type-name*. See
|
| 185 |
[[basic.lookup.elab]] and [[dcl.type.elab]]. — *end note*]
|
| 186 |
|
| 187 |
+
The point of declaration for an injected-class-name [[class.pre]] is
|
| 188 |
+
immediately following the opening brace of the class definition.
|
|
|
|
| 189 |
|
| 190 |
+
The point of declaration for a function-local predefined variable
|
| 191 |
+
[[dcl.fct.def.general]] is immediately before the *function-body* of a
|
| 192 |
+
function definition.
|
| 193 |
+
|
| 194 |
+
The point of declaration of a structured binding [[dcl.struct.bind]] is
|
| 195 |
+
immediately after the *identifier-list* of the structured binding
|
| 196 |
+
declaration.
|
| 197 |
+
|
| 198 |
+
The point of declaration for the variable or the structured bindings
|
| 199 |
+
declared in the *for-range-declaration* of a range-based `for` statement
|
| 200 |
+
[[stmt.ranged]] is immediately after the *for-range-initializer*.
|
| 201 |
|
| 202 |
The point of declaration for a template parameter is immediately after
|
| 203 |
its complete *template-parameter*.
|
| 204 |
|
| 205 |
[*Example 4*:
|
|
|
|
| 214 |
|
| 215 |
— *end example*]
|
| 216 |
|
| 217 |
[*Note 5*: Friend declarations refer to functions or classes that are
|
| 218 |
members of the nearest enclosing namespace, but they do not introduce
|
| 219 |
+
new names into that namespace [[namespace.memdef]]. Function
|
| 220 |
declarations at block scope and variable declarations with the `extern`
|
| 221 |
specifier at block scope refer to declarations that are members of an
|
| 222 |
enclosing namespace, but they do not introduce new names into that
|
| 223 |
scope. — *end note*]
|
| 224 |
|
| 225 |
[*Note 6*: For point of instantiation of a template, see
|
| 226 |
[[temp.point]]. — *end note*]
|
| 227 |
|
| 228 |
### Block scope <a id="basic.scope.block">[[basic.scope.block]]</a>
|
| 229 |
|
| 230 |
+
A name declared in a block [[stmt.block]] is local to that block; it has
|
| 231 |
+
*block scope*. Its potential scope begins at its point of declaration
|
| 232 |
+
[[basic.scope.pdecl]] and ends at the end of its block. A variable
|
| 233 |
+
declared at block scope is a *local variable*.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 234 |
|
| 235 |
The name declared in an *exception-declaration* is local to the
|
| 236 |
*handler* and shall not be redeclared in the outermost block of the
|
| 237 |
*handler*.
|
| 238 |
|
| 239 |
Names declared in the *init-statement*, the *for-range-declaration*, and
|
| 240 |
in the *condition* of `if`, `while`, `for`, and `switch` statements are
|
| 241 |
local to the `if`, `while`, `for`, or `switch` statement (including the
|
| 242 |
controlled statement), and shall not be redeclared in a subsequent
|
| 243 |
condition of that statement nor in the outermost block (or, for the `if`
|
| 244 |
+
statement, any of the outermost blocks) of the controlled statement.
|
|
|
|
| 245 |
|
| 246 |
+
[*Example 1*:
|
| 247 |
|
| 248 |
+
``` cpp
|
| 249 |
+
if (int x = f()) {
|
| 250 |
+
int x; // error: redeclaration of x
|
| 251 |
+
}
|
| 252 |
+
else {
|
| 253 |
+
int x; // error: redeclaration of x
|
| 254 |
+
}
|
| 255 |
+
```
|
| 256 |
+
|
| 257 |
+
— *end example*]
|
| 258 |
+
|
| 259 |
+
### Function parameter scope <a id="basic.scope.param">[[basic.scope.param]]</a>
|
| 260 |
+
|
| 261 |
+
A function parameter (including one appearing in a *lambda-declarator*)
|
| 262 |
+
or function-local predefined variable [[dcl.fct.def]] has *function
|
| 263 |
+
parameter scope*. The potential scope of a parameter or function-local
|
| 264 |
+
predefined variable begins at its point of declaration. If the nearest
|
| 265 |
+
enclosing function declarator is not the declarator of a function
|
| 266 |
+
definition, the potential scope ends at the end of that function
|
| 267 |
+
declarator. Otherwise, if the function has a *function-try-block* the
|
| 268 |
+
potential scope ends at the end of the last associated handler.
|
| 269 |
+
Otherwise the potential scope ends at the end of the outermost block of
|
| 270 |
+
the function definition. A parameter name shall not be redeclared in the
|
| 271 |
+
outermost block of the function definition nor in the outermost block of
|
| 272 |
+
any handler associated with a *function-try-block*.
|
| 273 |
|
| 274 |
### Function scope <a id="basic.funscope">[[basic.funscope]]</a>
|
| 275 |
|
| 276 |
+
Labels [[stmt.label]] have *function scope* and may be used anywhere in
|
| 277 |
+
the function in which they are declared. Only labels have function
|
| 278 |
scope.
|
| 279 |
|
| 280 |
### Namespace scope <a id="basic.scope.namespace">[[basic.scope.namespace]]</a>
|
| 281 |
|
| 282 |
The declarative region of a *namespace-definition* is its
|
| 283 |
*namespace-body*. Entities declared in a *namespace-body* are said to be
|
| 284 |
*members* of the namespace, and names introduced by these declarations
|
| 285 |
into the declarative region of the namespace are said to be *member
|
| 286 |
names* of the namespace. A namespace member name has namespace scope.
|
| 287 |
Its potential scope includes its namespace from the name’s point of
|
| 288 |
+
declaration [[basic.scope.pdecl]] onwards; and for each
|
| 289 |
+
*using-directive* [[namespace.udir]] that nominates the member’s
|
| 290 |
namespace, the member’s potential scope includes that portion of the
|
| 291 |
potential scope of the *using-directive* that follows the member’s point
|
| 292 |
of declaration.
|
| 293 |
|
| 294 |
[*Example 1*:
|
|
|
|
| 318 |
}
|
| 319 |
```
|
| 320 |
|
| 321 |
— *end example*]
|
| 322 |
|
| 323 |
+
If a translation unit Q is imported into a translation unit R
|
| 324 |
+
[[module.import]], the potential scope of a name X declared with
|
| 325 |
+
namespace scope in Q is extended to include the portion of the
|
| 326 |
+
corresponding namespace scope in R following the first
|
| 327 |
+
*module-import-declaration* or *module-declaration* in R that imports Q
|
| 328 |
+
(directly or indirectly) if
|
| 329 |
+
|
| 330 |
+
- X does not have internal linkage, and
|
| 331 |
+
- X is declared after the *module-declaration* in Q (if any), and
|
| 332 |
+
- either X is exported or Q and R are part of the same module.
|
| 333 |
+
|
| 334 |
+
[*Note 1*:
|
| 335 |
+
|
| 336 |
+
A *module-import-declaration* imports both the named translation unit(s)
|
| 337 |
+
and any modules named by exported *module-import-declaration*s within
|
| 338 |
+
them, recursively.
|
| 339 |
+
|
| 340 |
+
[*Example 2*:
|
| 341 |
+
|
| 342 |
+
Translation unit #1
|
| 343 |
+
|
| 344 |
+
``` cpp
|
| 345 |
+
export module Q;
|
| 346 |
+
export int sq(int i) { return i*i; }
|
| 347 |
+
```
|
| 348 |
+
|
| 349 |
+
Translation unit #2
|
| 350 |
+
|
| 351 |
+
``` cpp
|
| 352 |
+
export module R;
|
| 353 |
+
export import Q;
|
| 354 |
+
```
|
| 355 |
+
|
| 356 |
+
Translation unit #3
|
| 357 |
+
|
| 358 |
+
``` cpp
|
| 359 |
+
import R;
|
| 360 |
+
int main() { return sq(9); } // OK: sq from module Q
|
| 361 |
+
```
|
| 362 |
+
|
| 363 |
+
— *end example*]
|
| 364 |
+
|
| 365 |
+
— *end note*]
|
| 366 |
+
|
| 367 |
A namespace member can also be referred to after the `::` scope
|
| 368 |
+
resolution operator [[expr.prim.id.qual]] applied to the name of its
|
| 369 |
namespace or the name of a namespace which nominates the member’s
|
| 370 |
namespace in a *using-directive*; see [[namespace.qual]].
|
| 371 |
|
| 372 |
The outermost declarative region of a translation unit is also a
|
| 373 |
namespace, called the *global namespace*. A name declared in the global
|
| 374 |
namespace has *global namespace scope* (also called *global scope*). The
|
| 375 |
+
potential scope of such a name begins at its point of declaration
|
| 376 |
+
[[basic.scope.pdecl]] and ends at the end of the translation unit that
|
| 377 |
is its declarative region. A name with global namespace scope is said to
|
| 378 |
be a *global name*.
|
| 379 |
|
| 380 |
### Class scope <a id="basic.scope.class">[[basic.scope.class]]</a>
|
| 381 |
|
| 382 |
The potential scope of a name declared in a class consists not only of
|
| 383 |
the declarative region following the name’s point of declaration, but
|
| 384 |
+
also of all complete-class contexts [[class.mem]] of that class.
|
|
|
|
|
|
|
| 385 |
|
| 386 |
A name `N` used in a class `S` shall refer to the same declaration in
|
| 387 |
its context and when re-evaluated in the completed scope of `S`. No
|
| 388 |
diagnostic is required for a violation of this rule.
|
| 389 |
|
| 390 |
A name declared within a member function hides a declaration of the same
|
| 391 |
name whose scope extends to or past the end of the member function’s
|
| 392 |
class.
|
| 393 |
|
| 394 |
+
The potential scope of a declaration in a class that extends to or past
|
| 395 |
+
the end of a class definition also extends to the regions defined by its
|
| 396 |
+
member definitions, even if the members are defined lexically outside
|
| 397 |
+
the class (this includes static data member definitions, nested class
|
| 398 |
+
definitions, and member function definitions, including the member
|
| 399 |
+
function body and any portion of the declarator part of such definitions
|
| 400 |
+
which follows the *declarator-id*, including a
|
| 401 |
+
*parameter-declaration-clause* and any default arguments
|
| 402 |
+
[[dcl.fct.default]]).
|
| 403 |
|
| 404 |
[*Example 1*:
|
| 405 |
|
| 406 |
``` cpp
|
| 407 |
typedef int c;
|
|
|
|
| 430 |
— *end example*]
|
| 431 |
|
| 432 |
The name of a class member shall only be used as follows:
|
| 433 |
|
| 434 |
- in the scope of its class (as described above) or a class derived
|
| 435 |
+
[[class.derived]] from its class,
|
| 436 |
- after the `.` operator applied to an expression of the type of its
|
| 437 |
+
class [[expr.ref]] or a class derived from its class,
|
| 438 |
+
- after the `->` operator applied to a pointer to an object of its class
|
| 439 |
+
[[expr.ref]] or a class derived from its class,
|
| 440 |
+
- after the `::` scope resolution operator [[expr.prim.id.qual]] applied
|
| 441 |
+
to the name of its class or a class derived from its class.
|
| 442 |
|
| 443 |
### Enumeration scope <a id="basic.scope.enum">[[basic.scope.enum]]</a>
|
| 444 |
|
| 445 |
+
The name of a scoped enumerator [[dcl.enum]] has *enumeration scope*.
|
| 446 |
Its potential scope begins at its point of declaration and terminates at
|
| 447 |
the end of the *enum-specifier*.
|
| 448 |
|
| 449 |
### Template parameter scope <a id="basic.scope.temp">[[basic.scope.temp]]</a>
|
| 450 |
|
|
|
|
| 480 |
qualified and unqualified name lookup.)
|
| 481 |
|
| 482 |
— *end example*]
|
| 483 |
|
| 484 |
The potential scope of a template parameter name begins at its point of
|
| 485 |
+
declaration [[basic.scope.pdecl]] and ends at the end of its declarative
|
| 486 |
+
region.
|
| 487 |
|
| 488 |
[*Note 1*:
|
| 489 |
|
| 490 |
This implies that a *template-parameter* can be used in the declaration
|
| 491 |
of subsequent *template-parameter*s and their default arguments but
|
|
|
|
| 515 |
within the immediately-enclosing declarative region.
|
| 516 |
|
| 517 |
[*Note 2*:
|
| 518 |
|
| 519 |
As a result, a *template-parameter* hides any entity with the same name
|
| 520 |
+
in an enclosing scope [[basic.scope.hiding]].
|
| 521 |
|
| 522 |
[*Example 2*:
|
| 523 |
|
| 524 |
``` cpp
|
| 525 |
typedef int N;
|
|
|
|
| 533 |
— *end example*]
|
| 534 |
|
| 535 |
— *end note*]
|
| 536 |
|
| 537 |
[*Note 3*: Because the name of a template parameter cannot be
|
| 538 |
+
redeclared within its potential scope [[temp.local]], a template
|
| 539 |
parameter’s scope is often its potential scope. However, it is still
|
| 540 |
possible for a template parameter name to be hidden; see
|
| 541 |
[[temp.local]]. — *end note*]
|
| 542 |
|
| 543 |
### Name hiding <a id="basic.scope.hiding">[[basic.scope.hiding]]</a>
|
| 544 |
|
| 545 |
+
A declaration of a name in a nested declarative region hides a
|
| 546 |
+
declaration of the same name in an enclosing declarative region; see
|
| 547 |
+
[[basic.scope.declarative]] and [[basic.lookup.unqual]].
|
| 548 |
|
| 549 |
+
If a class name [[class.name]] or enumeration name [[dcl.enum]] and a
|
| 550 |
+
variable, data member, function, or enumerator are declared in the same
|
| 551 |
+
declarative region (in any order) with the same name (excluding
|
| 552 |
+
declarations made visible via *using-directive*s
|
| 553 |
+
[[basic.lookup.unqual]]), the class or enumeration name is hidden
|
| 554 |
+
wherever the variable, data member, function, or enumerator name is
|
| 555 |
+
visible.
|
| 556 |
|
| 557 |
In a member function definition, the declaration of a name at block
|
| 558 |
scope hides the declaration of a member of the class with the same name;
|
| 559 |
see [[basic.scope.class]]. The declaration of a member in a derived
|
| 560 |
+
class [[class.derived]] hides the declaration of a member of a base
|
| 561 |
+
class of the same name; see [[class.member.lookup]].
|
| 562 |
|
| 563 |
During the lookup of a name qualified by a namespace name, declarations
|
| 564 |
that would otherwise be made visible by a *using-directive* can be
|
| 565 |
hidden by declarations with the same name in the namespace containing
|
| 566 |
the *using-directive*; see [[namespace.qual]].
|