- tmp/tmp9nqphu35/{from.md → to.md} +267 -195
tmp/tmp9nqphu35/{from.md → to.md}
RENAMED
|
@@ -1,29 +1,29 @@
|
|
| 1 |
## Name lookup <a id="basic.lookup">[[basic.lookup]]</a>
|
| 2 |
|
| 3 |
The name lookup rules apply uniformly to all names (including
|
| 4 |
-
*typedef-name*s
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
place after name lookup has succeeded. The access
|
| 13 |
-
[[class.access]]
|
| 14 |
overload resolution (if applicable) have succeeded. Only after name
|
| 15 |
lookup, function overload resolution (if applicable) and access checking
|
| 16 |
-
have succeeded are the
|
| 17 |
-
|
|
|
|
| 18 |
|
| 19 |
-
A name “looked up in the context of an expression” is looked up
|
| 20 |
-
|
| 21 |
|
| 22 |
-
The injected-class-name of a class
|
| 23 |
-
|
| 24 |
-
and lookup.
|
| 25 |
|
| 26 |
[*Note 1*: [[basic.link]] discusses linkage issues. The notions of
|
| 27 |
scope, point of declaration and name hiding are discussed in
|
| 28 |
[[basic.scope]]. — *end note*]
|
| 29 |
|
|
@@ -46,12 +46,31 @@ function call is described in [[basic.lookup.argdep]].
|
|
| 46 |
|
| 47 |
[*Note 1*:
|
| 48 |
|
| 49 |
For purposes of determining (during parsing) whether an expression is a
|
| 50 |
*postfix-expression* for a function call, the usual name lookup rules
|
| 51 |
-
apply.
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
``` cpp
|
| 55 |
typedef int f;
|
| 56 |
namespace N {
|
| 57 |
struct A {
|
|
@@ -63,11 +82,11 @@ namespace N {
|
|
| 63 |
};
|
| 64 |
}
|
| 65 |
```
|
| 66 |
|
| 67 |
Because the expression is not a function call, the argument-dependent
|
| 68 |
-
name lookup
|
| 69 |
function `f` is not found.
|
| 70 |
|
| 71 |
— *end note*]
|
| 72 |
|
| 73 |
A name used in global scope, outside of any function, class or
|
|
@@ -77,15 +96,15 @@ scope.
|
|
| 77 |
A name used in a user-declared namespace outside of the definition of
|
| 78 |
any function or class shall be declared before its use in that namespace
|
| 79 |
or before its use in a namespace enclosing its namespace.
|
| 80 |
|
| 81 |
In the definition of a function that is a member of namespace `N`, a
|
| 82 |
-
name used after the function’s *declarator-id*[^
|
| 83 |
before its use in the block in which it is used or in one of its
|
| 84 |
-
enclosing blocks
|
| 85 |
-
|
| 86 |
-
|
| 87 |
|
| 88 |
[*Example 1*:
|
| 89 |
|
| 90 |
``` cpp
|
| 91 |
namespace A {
|
|
@@ -103,22 +122,21 @@ void A::N::f() {
|
|
| 103 |
}
|
| 104 |
```
|
| 105 |
|
| 106 |
— *end example*]
|
| 107 |
|
| 108 |
-
A name used in the definition of a class `X` outside of a
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
class definition[^5] shall be declared in one of the following ways:
|
| 112 |
|
| 113 |
-
- before its use in class `X` or be a member of a base class of `X`
|
| 114 |
-
[[class.member.lookup]]
|
| 115 |
-
- if `X` is a nested class of class `Y`
|
| 116 |
definition of `X` in `Y`, or shall be a member of a base class of `Y`
|
| 117 |
(this lookup applies in turn to `Y`’s enclosing classes, starting with
|
| 118 |
-
the innermost enclosing class),[^
|
| 119 |
-
- if `X` is a local class
|
| 120 |
local class, before the definition of class `X` in a block enclosing
|
| 121 |
the definition of class `X`, or
|
| 122 |
- if `X` is a member of namespace `N`, or is a nested class of a class
|
| 123 |
that is a member of `N`, or is a local class or a nested class within
|
| 124 |
a local class of a function that is a member of `N`, before the
|
|
@@ -151,36 +169,34 @@ namespace N {
|
|
| 151 |
```
|
| 152 |
|
| 153 |
— *end example*]
|
| 154 |
|
| 155 |
[*Note 2*: When looking for a prior declaration of a class or function
|
| 156 |
-
introduced by a
|
| 157 |
enclosing namespace scope are not considered; see
|
| 158 |
[[namespace.memdef]]. — *end note*]
|
| 159 |
|
| 160 |
[*Note 3*: [[basic.scope.class]] further describes the restrictions on
|
| 161 |
the use of names in a class definition. [[class.nest]] further describes
|
| 162 |
the restrictions on the use of names in nested class definitions.
|
| 163 |
[[class.local]] further describes the restrictions on the use of names
|
| 164 |
in local class definitions. — *end note*]
|
| 165 |
|
| 166 |
-
For the members of a class `X`, a name used in a
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
definition of `X`, following the member’s *declarator-id*[^7], shall be
|
| 171 |
-
declared in one of the following ways:
|
| 172 |
|
| 173 |
- before its use in the block in which it is used or in an enclosing
|
| 174 |
-
block
|
| 175 |
-
- shall be a member of class `X` or be a member of a base class of `X`
|
| 176 |
-
[[class.member.lookup]]
|
| 177 |
-
- if `X` is a nested class of class `Y`
|
| 178 |
member of `Y`, or shall be a member of a base class of `Y` (this
|
| 179 |
lookup applies in turn to `Y`’s enclosing classes, starting with the
|
| 180 |
-
innermost enclosing class),[^
|
| 181 |
-
- if `X` is a local class
|
| 182 |
local class, before the definition of class `X` in a block enclosing
|
| 183 |
the definition of class `X`, or
|
| 184 |
- if `X` is a member of namespace `N`, or is a nested class of a class
|
| 185 |
that is a member of `N`, or is a local class or a nested class within
|
| 186 |
a local class of a function that is a member of `N`, before the use of
|
|
@@ -217,21 +233,21 @@ restrictions on the use of names in member function definitions.
|
|
| 217 |
[[class.nest]] further describes the restrictions on the use of names in
|
| 218 |
the scope of nested classes. [[class.local]] further describes the
|
| 219 |
restrictions on the use of names in local class
|
| 220 |
definitions. — *end note*]
|
| 221 |
|
| 222 |
-
Name lookup for a name used in the definition of a
|
| 223 |
-
[[class.friend]]
|
| 224 |
proceed as described for lookup in member function definitions. If the
|
| 225 |
-
|
| 226 |
-
lookup in the
|
| 227 |
-
|
| 228 |
|
| 229 |
-
In a
|
| 230 |
function declarator and not part of a *template-argument* in the
|
| 231 |
*declarator-id* is first looked up in the scope of the member function’s
|
| 232 |
-
class
|
| 233 |
part of a *template-argument* in the *declarator-id*, the look up is as
|
| 234 |
described for unqualified names in the definition of the class granting
|
| 235 |
friendship.
|
| 236 |
|
| 237 |
[*Example 4*:
|
|
@@ -252,14 +268,14 @@ struct B {
|
|
| 252 |
};
|
| 253 |
```
|
| 254 |
|
| 255 |
— *end example*]
|
| 256 |
|
| 257 |
-
During the lookup for a name used as a default argument
|
| 258 |
-
[[dcl.fct.default]]
|
| 259 |
-
|
| 260 |
-
[[class.base.init]]
|
| 261 |
the names of entities declared in the block, class or namespace scopes
|
| 262 |
containing the function declaration.
|
| 263 |
|
| 264 |
[*Note 5*: [[dcl.fct.default]] further describes the restrictions on
|
| 265 |
the use of names in default arguments. [[class.base.init]] further
|
|
@@ -269,13 +285,13 @@ describes the restrictions on the use of names in a
|
|
| 269 |
During the lookup of a name used in the *constant-expression* of an
|
| 270 |
*enumerator-definition*, previously declared *enumerator*s of the
|
| 271 |
enumeration are visible and hide the names of entities declared in the
|
| 272 |
block, class, or namespace scopes containing the *enum-specifier*.
|
| 273 |
|
| 274 |
-
A name used in the definition of a `static` data member of class `X`
|
| 275 |
-
[[class.static.data]]
|
| 276 |
-
|
| 277 |
|
| 278 |
[*Note 6*: [[class.static.data]] further describes the restrictions on
|
| 279 |
the use of names in the definition of a `static` data
|
| 280 |
member. — *end note*]
|
| 281 |
|
|
@@ -297,32 +313,32 @@ int i = 2;
|
|
| 297 |
int N::j = i; // N::j == 4
|
| 298 |
```
|
| 299 |
|
| 300 |
— *end example*]
|
| 301 |
|
| 302 |
-
A name used in the handler for a *function-try-block*
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
|
| 310 |
[*Note 7*: But function parameter names are found. — *end note*]
|
| 311 |
|
| 312 |
[*Note 8*: The rules for name lookup in template definitions are
|
| 313 |
described in [[temp.res]]. — *end note*]
|
| 314 |
|
| 315 |
### Argument-dependent name lookup <a id="basic.lookup.argdep">[[basic.lookup.argdep]]</a>
|
| 316 |
|
| 317 |
-
When the *postfix-expression* in a function call
|
| 318 |
*unqualified-id*, other namespaces not considered during the usual
|
| 319 |
-
unqualified lookup
|
| 320 |
-
|
| 321 |
-
declarations
|
| 322 |
-
|
| 323 |
-
|
| 324 |
argument).
|
| 325 |
|
| 326 |
[*Example 1*:
|
| 327 |
|
| 328 |
``` cpp
|
|
@@ -340,62 +356,62 @@ void g() {
|
|
| 340 |
|
| 341 |
— *end example*]
|
| 342 |
|
| 343 |
For each argument type `T` in the function call, there is a set of zero
|
| 344 |
or more *associated namespaces* and a set of zero or more *associated
|
| 345 |
-
|
| 346 |
-
determined entirely by the types of the
|
| 347 |
-
namespace of any template template
|
| 348 |
-
*using-declaration*s used to specify the
|
| 349 |
-
set. The sets of namespaces and
|
| 350 |
-
way:
|
| 351 |
|
| 352 |
- If `T` is a fundamental type, its associated sets of namespaces and
|
| 353 |
-
|
| 354 |
-
- If `T` is a class type (including unions), its associated
|
| 355 |
-
the class itself; the class of which it is a member, if any; and
|
| 356 |
-
direct and indirect base classes. Its associated namespaces are
|
| 357 |
-
innermost enclosing namespaces of its associated
|
| 358 |
-
if `T` is a class template specialization, its associated
|
| 359 |
-
and
|
| 360 |
-
the types of the template arguments provided for
|
| 361 |
-
parameters (excluding template template parameters); the
|
| 362 |
-
|
| 363 |
-
|
| 364 |
-
|
| 365 |
-
|
|
|
|
| 366 |
- If `T` is an enumeration type, its associated namespace is the
|
| 367 |
-
innermost enclosing namespace of its declaration
|
| 368 |
-
|
| 369 |
-
associated class.
|
| 370 |
- If `T` is a pointer to `U` or an array of `U`, its associated
|
| 371 |
-
namespaces and
|
| 372 |
-
- If `T` is a function type, its associated namespaces and
|
| 373 |
those associated with the function parameter types and those
|
| 374 |
associated with the return type.
|
| 375 |
- If `T` is a pointer to a member function of a class `X`, its
|
| 376 |
-
associated namespaces and
|
| 377 |
function parameter types and return type, together with those
|
| 378 |
associated with `X`.
|
| 379 |
- If `T` is a pointer to a data member of class `X`, its associated
|
| 380 |
-
namespaces and
|
| 381 |
together with those associated with `X`.
|
| 382 |
|
| 383 |
-
If an associated namespace is an inline namespace
|
| 384 |
-
|
| 385 |
namespace directly contains inline namespaces, those inline namespaces
|
| 386 |
are also included in the set. In addition, if the argument is the name
|
| 387 |
-
or address of
|
| 388 |
-
|
| 389 |
-
|
| 390 |
-
|
| 391 |
-
|
| 392 |
-
*template-
|
| 393 |
-
|
| 394 |
|
| 395 |
-
Let *X* be the lookup set produced by unqualified lookup
|
| 396 |
-
[[basic.lookup.unqual]]
|
| 397 |
argument dependent lookup (defined as follows). If *X* contains
|
| 398 |
|
| 399 |
- a declaration of a class member, or
|
| 400 |
- a block-scope function declaration that is not a *using-declaration*,
|
| 401 |
or
|
|
@@ -404,12 +420,12 @@ argument dependent lookup (defined as follows). If *X* contains
|
|
| 404 |
then *Y* is empty. Otherwise *Y* is the set of declarations found in the
|
| 405 |
namespaces associated with the argument types as described below. The
|
| 406 |
set of declarations found by the lookup of the name is the union of *X*
|
| 407 |
and *Y*.
|
| 408 |
|
| 409 |
-
[*Note 2*: The namespaces and
|
| 410 |
-
types can include namespaces and
|
| 411 |
ordinary unqualified lookup. — *end note*]
|
| 412 |
|
| 413 |
[*Example 2*:
|
| 414 |
|
| 415 |
``` cpp
|
|
@@ -427,27 +443,88 @@ int main() {
|
|
| 427 |
}
|
| 428 |
```
|
| 429 |
|
| 430 |
— *end example*]
|
| 431 |
|
| 432 |
-
When considering an associated namespace, the lookup is the same as
|
| 433 |
-
lookup performed when
|
| 434 |
-
|
| 435 |
|
| 436 |
-
- Any *using-directive*s in
|
| 437 |
-
- Any namespace-scope friend functions or friend function templates
|
| 438 |
-
declared in associated classes are visible within their respective
|
| 439 |
-
namespaces even if they are not visible during an ordinary lookup (
|
| 440 |
-
[[class.friend]]).
|
| 441 |
- All names except those of (possibly overloaded) functions and function
|
| 442 |
templates are ignored.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 443 |
|
| 444 |
### Qualified name lookup <a id="basic.lookup.qual">[[basic.lookup.qual]]</a>
|
| 445 |
|
| 446 |
The name of a class or namespace member or enumerator can be referred to
|
| 447 |
-
after the `::` scope resolution operator
|
| 448 |
-
*nested-name-specifier* that denotes its class, namespace, or
|
| 449 |
enumeration. If a `::` scope resolution operator in a
|
| 450 |
*nested-name-specifier* is not preceded by a *decltype-specifier*,
|
| 451 |
lookup of the name preceding that `::` considers only namespaces, types,
|
| 452 |
and templates whose specializations are types. If the name found does
|
| 453 |
not designate a namespace or a class, enumeration, or dependent type,
|
|
@@ -461,19 +538,19 @@ public:
|
|
| 461 |
static int n;
|
| 462 |
};
|
| 463 |
int main() {
|
| 464 |
int A;
|
| 465 |
A::n = 42; // OK
|
| 466 |
-
A b; //
|
| 467 |
}
|
| 468 |
```
|
| 469 |
|
| 470 |
— *end example*]
|
| 471 |
|
| 472 |
[*Note 1*: Multiply qualified names, such as `N1::N2::N3::n`, can be
|
| 473 |
-
used to refer to members of nested classes
|
| 474 |
-
|
| 475 |
|
| 476 |
In a declaration in which the *declarator-id* is a *qualified-id*, names
|
| 477 |
used before the *qualified-id* being declared are looked up in the
|
| 478 |
defining namespace scope; names following the *qualified-id* are looked
|
| 479 |
up in the scope of the member’s class or namespace.
|
|
@@ -485,38 +562,35 @@ class X { };
|
|
| 485 |
class C {
|
| 486 |
class X { };
|
| 487 |
static const int number = 50;
|
| 488 |
static X arr[number];
|
| 489 |
};
|
| 490 |
-
X C::arr[number]; //
|
| 491 |
// equivalent to ::X C::arr[C::number];
|
| 492 |
// and not to C::X C::arr[C::number];
|
| 493 |
```
|
| 494 |
|
| 495 |
— *end example*]
|
| 496 |
|
| 497 |
-
A name prefixed by the unary scope operator `::`
|
| 498 |
-
looked up in global scope, in the translation unit where it is used.
|
| 499 |
-
name shall be declared in global namespace scope or shall be a name
|
| 500 |
whose declaration is visible in global scope because of a
|
| 501 |
-
*using-directive*
|
| 502 |
-
name to be referred to even if its identifier has been hidden
|
| 503 |
-
[[basic.scope.hiding]]
|
| 504 |
|
| 505 |
A name prefixed by a *nested-name-specifier* that nominates an
|
| 506 |
enumeration type shall represent an *enumerator* of that enumeration.
|
| 507 |
|
| 508 |
-
|
| 509 |
-
*nested-name-specifier*, the *type-name*s are looked up as types in the
|
| 510 |
-
scope designated by the *nested-name-specifier*. Similarly, in a
|
| 511 |
-
*qualified-id* of the form:
|
| 512 |
|
| 513 |
``` bnf
|
| 514 |
-
nested-name-specifierₒₚₜ
|
| 515 |
```
|
| 516 |
|
| 517 |
-
the second *
|
| 518 |
|
| 519 |
[*Example 3*:
|
| 520 |
|
| 521 |
``` cpp
|
| 522 |
struct C {
|
|
@@ -545,40 +619,39 @@ proceeds after the `.` and `->` operators. — *end note*]
|
|
| 545 |
|
| 546 |
#### Class members <a id="class.qual">[[class.qual]]</a>
|
| 547 |
|
| 548 |
If the *nested-name-specifier* of a *qualified-id* nominates a class,
|
| 549 |
the name specified after the *nested-name-specifier* is looked up in the
|
| 550 |
-
scope of the class
|
| 551 |
-
|
| 552 |
-
|
| 553 |
|
| 554 |
[*Note 1*: A class member can be referred to using a *qualified-id* at
|
| 555 |
-
any point in its potential scope
|
| 556 |
-
[[basic.scope.class]]). — *end note*]
|
| 557 |
|
| 558 |
The exceptions to the name lookup rule above are the following:
|
| 559 |
|
| 560 |
- the lookup for a destructor is as specified in [[basic.lookup.qual]];
|
| 561 |
- a *conversion-type-id* of a *conversion-function-id* is looked up in
|
| 562 |
the same manner as a *conversion-type-id* in a class member access
|
| 563 |
(see [[basic.lookup.classref]]);
|
| 564 |
- the names in a *template-argument* of a *template-id* are looked up in
|
| 565 |
-
the context in which the entire *postfix-expression* occurs
|
| 566 |
-
- the lookup for a name specified in a *using-declaration*
|
| 567 |
-
[[namespace.udecl]]
|
| 568 |
-
within the same scope
|
| 569 |
|
| 570 |
-
In a lookup in which function names are not ignored[^
|
| 571 |
*nested-name-specifier* nominates a class `C`:
|
| 572 |
|
| 573 |
- if the name specified after the *nested-name-specifier*, when looked
|
| 574 |
-
up in `C`, is the injected-class-name of `C`
|
| 575 |
-
- in a *using-declarator* of a *using-declaration*
|
| 576 |
-
|
| 577 |
-
|
| 578 |
-
*
|
| 579 |
-
|
| 580 |
|
| 581 |
the name is instead considered to name the constructor of class `C`.
|
| 582 |
|
| 583 |
[*Note 2*: For example, the constructor is not an acceptable lookup
|
| 584 |
result in an *elaborated-type-specifier* so the constructor would not be
|
|
@@ -595,11 +668,11 @@ struct B: public A { B(); };
|
|
| 595 |
|
| 596 |
A::A() { }
|
| 597 |
B::B() { }
|
| 598 |
|
| 599 |
B::A ba; // object of type A
|
| 600 |
-
A::A a; // error
|
| 601 |
struct A::A a2; // object of type A
|
| 602 |
```
|
| 603 |
|
| 604 |
— *end example*]
|
| 605 |
|
|
@@ -616,22 +689,24 @@ nominating the global namespace), the name specified after the
|
|
| 616 |
names in a *template-argument* of a *template-id* are looked up in the
|
| 617 |
context in which the entire *postfix-expression* occurs.
|
| 618 |
|
| 619 |
For a namespace `X` and name `m`, the namespace-qualified lookup set
|
| 620 |
S(X, m) is defined as follows: Let S'(X, m) be the set of all
|
| 621 |
-
declarations of `m` in `X` and the inline namespace set of `X`
|
| 622 |
-
[[namespace.def]]
|
|
|
|
|
|
|
| 623 |
otherwise, S(X, m) is the union of S(Nᵢ, m) for all namespaces Nᵢ
|
| 624 |
nominated by *using-directive*s in `X` and its inline namespace set.
|
| 625 |
|
| 626 |
Given `X::m` (where `X` is a user-declared namespace), or given `::m`
|
| 627 |
(where X is the global namespace), if S(X, m) is the empty set, the
|
| 628 |
program is ill-formed. Otherwise, if S(X, m) has exactly one member, or
|
| 629 |
-
if the context of the reference is a *using-declaration*
|
| 630 |
-
[[namespace.udecl]]
|
| 631 |
-
|
| 632 |
-
|
| 633 |
|
| 634 |
[*Example 1*:
|
| 635 |
|
| 636 |
``` cpp
|
| 637 |
int x;
|
|
@@ -767,11 +842,11 @@ void f()
|
|
| 767 |
— *end example*]
|
| 768 |
|
| 769 |
During the lookup of a qualified namespace member name, if the lookup
|
| 770 |
finds more than one declaration of the member, and if one declaration
|
| 771 |
introduces a class name or enumeration name and the other declarations
|
| 772 |
-
|
| 773 |
functions, the non-type name hides the class or enumeration name if and
|
| 774 |
only if the declarations are from the same namespace; otherwise (the
|
| 775 |
declarations are from different namespaces), the program is ill-formed.
|
| 776 |
|
| 777 |
[*Example 4*:
|
|
@@ -804,23 +879,23 @@ has the form
|
|
| 804 |
``` bnf
|
| 805 |
nested-name-specifier unqualified-id
|
| 806 |
```
|
| 807 |
|
| 808 |
the *unqualified-id* shall name a member of the namespace designated by
|
| 809 |
-
the *nested-name-specifier* or of an element of the inline namespace
|
| 810 |
-
|
| 811 |
|
| 812 |
[*Example 5*:
|
| 813 |
|
| 814 |
``` cpp
|
| 815 |
namespace A {
|
| 816 |
namespace B {
|
| 817 |
void f1(int);
|
| 818 |
}
|
| 819 |
using namespace B;
|
| 820 |
}
|
| 821 |
-
void A::f1(int){ } //
|
| 822 |
```
|
| 823 |
|
| 824 |
— *end example*]
|
| 825 |
|
| 826 |
However, in such namespace member declarations, the
|
|
@@ -849,14 +924,13 @@ void B::f1(int){ } // OK, defines A::B::f1(int)
|
|
| 849 |
|
| 850 |
— *end example*]
|
| 851 |
|
| 852 |
### Elaborated type specifiers <a id="basic.lookup.elab">[[basic.lookup.elab]]</a>
|
| 853 |
|
| 854 |
-
An *elaborated-type-specifier*
|
| 855 |
-
|
| 856 |
-
|
| 857 |
-
[[basic.scope.hiding]]).
|
| 858 |
|
| 859 |
If the *elaborated-type-specifier* has no *nested-name-specifier*, and
|
| 860 |
unless the *elaborated-type-specifier* appears in a declaration with the
|
| 861 |
following form:
|
| 862 |
|
|
@@ -888,18 +962,17 @@ declared. If the name lookup does not find a previously declared
|
|
| 888 |
|
| 889 |
[*Example 1*:
|
| 890 |
|
| 891 |
``` cpp
|
| 892 |
struct Node {
|
| 893 |
-
struct Node* Next; // OK: Refers to
|
| 894 |
-
struct Data* Data; // OK: Declares type Data
|
| 895 |
-
// at global scope and member Data
|
| 896 |
};
|
| 897 |
|
| 898 |
struct Data {
|
| 899 |
struct Node* Node; // OK: Refers to Node at global scope
|
| 900 |
-
friend struct ::Glob; // error: Glob is not declared, cannot introduce a qualified type
|
| 901 |
friend struct Glob; // OK: Refers to (as yet) undeclared Glob at global scope.
|
| 902 |
...
|
| 903 |
};
|
| 904 |
|
| 905 |
struct Base {
|
|
@@ -910,34 +983,33 @@ struct Base {
|
|
| 910 |
friend class Data; // OK: nested Data is a friend
|
| 911 |
struct Data { ... }; // Defines nested Data
|
| 912 |
};
|
| 913 |
|
| 914 |
struct Data; // OK: Redeclares Data at global scope
|
| 915 |
-
struct ::Data; // error: cannot introduce a qualified type
|
| 916 |
-
struct Base::Data; // error: cannot introduce a qualified type
|
| 917 |
struct Base::Datum; // error: Datum undefined
|
| 918 |
struct Base::Data* pBase; // OK: refers to nested Data
|
| 919 |
```
|
| 920 |
|
| 921 |
— *end example*]
|
| 922 |
|
| 923 |
### Class member access <a id="basic.lookup.classref">[[basic.lookup.classref]]</a>
|
| 924 |
|
| 925 |
-
In a class member access expression
|
| 926 |
token is immediately followed by an *identifier* followed by a `<`, the
|
| 927 |
identifier must be looked up to determine whether the `<` is the
|
| 928 |
-
beginning of a template argument list
|
| 929 |
operator. The identifier is first looked up in the class of the object
|
| 930 |
-
expression. If the identifier is not found, it
|
| 931 |
-
context of the entire *postfix-expression* and
|
| 932 |
-
template.
|
| 933 |
|
| 934 |
-
If the *id-expression* in a class member access
|
| 935 |
*unqualified-id*, and the type of the object expression is of a class
|
| 936 |
-
type `C`, the *unqualified-id* is looked up in the scope of class `C`
|
| 937 |
-
|
| 938 |
-
looked up in the context of the complete *postfix-expression*.
|
| 939 |
|
| 940 |
If the *unqualified-id* is `~`*type-name*, the *type-name* is looked up
|
| 941 |
in the context of the entire *postfix-expression*. If the type `T` of
|
| 942 |
the object expression is of a class type `C`, the *type-name* is also
|
| 943 |
looked up in the scope of class `C`. At least one of the lookups shall
|
|
@@ -965,14 +1037,14 @@ the form
|
|
| 965 |
|
| 966 |
``` cpp
|
| 967 |
class-name-or-namespace-name::...
|
| 968 |
```
|
| 969 |
|
| 970 |
-
the
|
| 971 |
-
first looked up in the class of the object expression
|
| 972 |
-
found, is used. Otherwise it is
|
| 973 |
-
*postfix-expression*.
|
| 974 |
|
| 975 |
[*Note 1*: See [[basic.lookup.qual]], which describes the lookup of a
|
| 976 |
name before `::`, which will only find a type or namespace
|
| 977 |
name. — *end note*]
|
| 978 |
|
|
@@ -980,23 +1052,23 @@ If the *qualified-id* has the form
|
|
| 980 |
|
| 981 |
``` cpp
|
| 982 |
::class-name-or-namespace-name::...
|
| 983 |
```
|
| 984 |
|
| 985 |
-
the
|
| 986 |
*class-name* or *namespace-name*.
|
| 987 |
|
| 988 |
-
If the *nested-name-specifier* contains a *simple-template-id*
|
| 989 |
-
[[temp.names]]
|
| 990 |
the context in which the entire *postfix-expression* occurs.
|
| 991 |
|
| 992 |
If the *id-expression* is a *conversion-function-id*, its
|
| 993 |
*conversion-type-id* is first looked up in the class of the object
|
| 994 |
-
expression and the name, if found, is used.
|
| 995 |
-
|
| 996 |
-
lookups, only names that denote
|
| 997 |
-
are types are considered.
|
| 998 |
|
| 999 |
[*Example 2*:
|
| 1000 |
|
| 1001 |
``` cpp
|
| 1002 |
struct A { };
|
|
|
|
| 1 |
## Name lookup <a id="basic.lookup">[[basic.lookup]]</a>
|
| 2 |
|
| 3 |
The name lookup rules apply uniformly to all names (including
|
| 4 |
+
*typedef-name*s [[dcl.typedef]], *namespace-name*s [[basic.namespace]],
|
| 5 |
+
and *class-name*s [[class.name]]) wherever the grammar allows such names
|
| 6 |
+
in the context discussed by a particular rule. Name lookup associates
|
| 7 |
+
the use of a name with a set of declarations [[basic.def]] of that name.
|
| 8 |
+
If the declarations found by name lookup all denote functions or
|
| 9 |
+
function templates, the declarations are said to form an *overload set*.
|
| 10 |
+
The declarations found by name lookup shall either all denote the same
|
| 11 |
+
entity or form an overload set. Overload resolution ([[over.match]],
|
| 12 |
+
[[over.over]]) takes place after name lookup has succeeded. The access
|
| 13 |
+
rules [[class.access]] are considered only once name lookup and function
|
| 14 |
overload resolution (if applicable) have succeeded. Only after name
|
| 15 |
lookup, function overload resolution (if applicable) and access checking
|
| 16 |
+
have succeeded are the semantic properties introduced by the name’s
|
| 17 |
+
declaration and its reachable [[module.reach]] redeclarations used
|
| 18 |
+
further in expression processing [[expr]].
|
| 19 |
|
| 20 |
+
A name “looked up in the context of an expression” is looked up in the
|
| 21 |
+
scope where the expression is found.
|
| 22 |
|
| 23 |
+
The injected-class-name of a class [[class.pre]] is also considered to
|
| 24 |
+
be a member of that class for the purposes of name hiding and lookup.
|
|
|
|
| 25 |
|
| 26 |
[*Note 1*: [[basic.link]] discusses linkage issues. The notions of
|
| 27 |
scope, point of declaration and name hiding are discussed in
|
| 28 |
[[basic.scope]]. — *end note*]
|
| 29 |
|
|
|
|
| 46 |
|
| 47 |
[*Note 1*:
|
| 48 |
|
| 49 |
For purposes of determining (during parsing) whether an expression is a
|
| 50 |
*postfix-expression* for a function call, the usual name lookup rules
|
| 51 |
+
apply. In some cases a name followed by `<` is treated as a
|
| 52 |
+
*template-name* even though name lookup did not find a *template-name*
|
| 53 |
+
(see [[temp.names]]). For example,
|
| 54 |
+
|
| 55 |
+
``` cpp
|
| 56 |
+
int h;
|
| 57 |
+
void g();
|
| 58 |
+
namespace N {
|
| 59 |
+
struct A {};
|
| 60 |
+
template <class T> int f(T);
|
| 61 |
+
template <class T> int g(T);
|
| 62 |
+
template <class T> int h(T);
|
| 63 |
+
}
|
| 64 |
+
|
| 65 |
+
int x = f<N::A>(N::A()); // OK: lookup of f finds nothing, f treated as template name
|
| 66 |
+
int y = g<N::A>(N::A()); // OK: lookup of g finds a function, g treated as template name
|
| 67 |
+
int z = h<N::A>(N::A()); // error: h< does not begin a template-id
|
| 68 |
+
```
|
| 69 |
+
|
| 70 |
+
The rules in [[basic.lookup.argdep]] have no effect on the syntactic
|
| 71 |
+
interpretation of an expression. For example,
|
| 72 |
|
| 73 |
``` cpp
|
| 74 |
typedef int f;
|
| 75 |
namespace N {
|
| 76 |
struct A {
|
|
|
|
| 82 |
};
|
| 83 |
}
|
| 84 |
```
|
| 85 |
|
| 86 |
Because the expression is not a function call, the argument-dependent
|
| 87 |
+
name lookup [[basic.lookup.argdep]] does not apply and the friend
|
| 88 |
function `f` is not found.
|
| 89 |
|
| 90 |
— *end note*]
|
| 91 |
|
| 92 |
A name used in global scope, outside of any function, class or
|
|
|
|
| 96 |
A name used in a user-declared namespace outside of the definition of
|
| 97 |
any function or class shall be declared before its use in that namespace
|
| 98 |
or before its use in a namespace enclosing its namespace.
|
| 99 |
|
| 100 |
In the definition of a function that is a member of namespace `N`, a
|
| 101 |
+
name used after the function’s *declarator-id*[^3] shall be declared
|
| 102 |
before its use in the block in which it is used or in one of its
|
| 103 |
+
enclosing blocks [[stmt.block]] or shall be declared before its use in
|
| 104 |
+
namespace `N` or, if `N` is a nested namespace, shall be declared before
|
| 105 |
+
its use in one of `N`’s enclosing namespaces.
|
| 106 |
|
| 107 |
[*Example 1*:
|
| 108 |
|
| 109 |
``` cpp
|
| 110 |
namespace A {
|
|
|
|
| 122 |
}
|
| 123 |
```
|
| 124 |
|
| 125 |
— *end example*]
|
| 126 |
|
| 127 |
+
A name used in the definition of a class `X` [^4] outside of a
|
| 128 |
+
complete-class context [[class.mem]] of `X` shall be declared in one of
|
| 129 |
+
the following ways:
|
|
|
|
| 130 |
|
| 131 |
+
- before its use in class `X` or be a member of a base class of `X`
|
| 132 |
+
[[class.member.lookup]], or
|
| 133 |
+
- if `X` is a nested class of class `Y` [[class.nest]], before the
|
| 134 |
definition of `X` in `Y`, or shall be a member of a base class of `Y`
|
| 135 |
(this lookup applies in turn to `Y`’s enclosing classes, starting with
|
| 136 |
+
the innermost enclosing class),[^5] or
|
| 137 |
+
- if `X` is a local class [[class.local]] or is a nested class of a
|
| 138 |
local class, before the definition of class `X` in a block enclosing
|
| 139 |
the definition of class `X`, or
|
| 140 |
- if `X` is a member of namespace `N`, or is a nested class of a class
|
| 141 |
that is a member of `N`, or is a local class or a nested class within
|
| 142 |
a local class of a function that is a member of `N`, before the
|
|
|
|
| 169 |
```
|
| 170 |
|
| 171 |
— *end example*]
|
| 172 |
|
| 173 |
[*Note 2*: When looking for a prior declaration of a class or function
|
| 174 |
+
introduced by a friend declaration, scopes outside of the innermost
|
| 175 |
enclosing namespace scope are not considered; see
|
| 176 |
[[namespace.memdef]]. — *end note*]
|
| 177 |
|
| 178 |
[*Note 3*: [[basic.scope.class]] further describes the restrictions on
|
| 179 |
the use of names in a class definition. [[class.nest]] further describes
|
| 180 |
the restrictions on the use of names in nested class definitions.
|
| 181 |
[[class.local]] further describes the restrictions on the use of names
|
| 182 |
in local class definitions. — *end note*]
|
| 183 |
|
| 184 |
+
For the members of a class `X`, a name used in a complete-class context
|
| 185 |
+
[[class.mem]] of `X` or in the definition of a class member outside of
|
| 186 |
+
the definition of `X`, following the member’s *declarator-id*[^6], shall
|
| 187 |
+
be declared in one of the following ways:
|
|
|
|
|
|
|
| 188 |
|
| 189 |
- before its use in the block in which it is used or in an enclosing
|
| 190 |
+
block [[stmt.block]], or
|
| 191 |
+
- shall be a member of class `X` or be a member of a base class of `X`
|
| 192 |
+
[[class.member.lookup]], or
|
| 193 |
+
- if `X` is a nested class of class `Y` [[class.nest]], shall be a
|
| 194 |
member of `Y`, or shall be a member of a base class of `Y` (this
|
| 195 |
lookup applies in turn to `Y`’s enclosing classes, starting with the
|
| 196 |
+
innermost enclosing class),[^7] or
|
| 197 |
+
- if `X` is a local class [[class.local]] or is a nested class of a
|
| 198 |
local class, before the definition of class `X` in a block enclosing
|
| 199 |
the definition of class `X`, or
|
| 200 |
- if `X` is a member of namespace `N`, or is a nested class of a class
|
| 201 |
that is a member of `N`, or is a local class or a nested class within
|
| 202 |
a local class of a function that is a member of `N`, before the use of
|
|
|
|
| 233 |
[[class.nest]] further describes the restrictions on the use of names in
|
| 234 |
the scope of nested classes. [[class.local]] further describes the
|
| 235 |
restrictions on the use of names in local class
|
| 236 |
definitions. — *end note*]
|
| 237 |
|
| 238 |
+
Name lookup for a name used in the definition of a friend function
|
| 239 |
+
[[class.friend]] defined inline in the class granting friendship shall
|
| 240 |
proceed as described for lookup in member function definitions. If the
|
| 241 |
+
friend function is not defined in the class granting friendship, name
|
| 242 |
+
lookup in the friend function definition shall proceed as described for
|
| 243 |
+
lookup in namespace member function definitions.
|
| 244 |
|
| 245 |
+
In a friend declaration naming a member function, a name used in the
|
| 246 |
function declarator and not part of a *template-argument* in the
|
| 247 |
*declarator-id* is first looked up in the scope of the member function’s
|
| 248 |
+
class [[class.member.lookup]]. If it is not found, or if the name is
|
| 249 |
part of a *template-argument* in the *declarator-id*, the look up is as
|
| 250 |
described for unqualified names in the definition of the class granting
|
| 251 |
friendship.
|
| 252 |
|
| 253 |
[*Example 4*:
|
|
|
|
| 268 |
};
|
| 269 |
```
|
| 270 |
|
| 271 |
— *end example*]
|
| 272 |
|
| 273 |
+
During the lookup for a name used as a default argument
|
| 274 |
+
[[dcl.fct.default]] in a function *parameter-declaration-clause* or used
|
| 275 |
+
in the *expression* of a *mem-initializer* for a constructor
|
| 276 |
+
[[class.base.init]], the function parameter names are visible and hide
|
| 277 |
the names of entities declared in the block, class or namespace scopes
|
| 278 |
containing the function declaration.
|
| 279 |
|
| 280 |
[*Note 5*: [[dcl.fct.default]] further describes the restrictions on
|
| 281 |
the use of names in default arguments. [[class.base.init]] further
|
|
|
|
| 285 |
During the lookup of a name used in the *constant-expression* of an
|
| 286 |
*enumerator-definition*, previously declared *enumerator*s of the
|
| 287 |
enumeration are visible and hide the names of entities declared in the
|
| 288 |
block, class, or namespace scopes containing the *enum-specifier*.
|
| 289 |
|
| 290 |
+
A name used in the definition of a `static` data member of class `X`
|
| 291 |
+
[[class.static.data]] (after the *qualified-id* of the static member) is
|
| 292 |
+
looked up as if the name was used in a member function of `X`.
|
| 293 |
|
| 294 |
[*Note 6*: [[class.static.data]] further describes the restrictions on
|
| 295 |
the use of names in the definition of a `static` data
|
| 296 |
member. — *end note*]
|
| 297 |
|
|
|
|
| 313 |
int N::j = i; // N::j == 4
|
| 314 |
```
|
| 315 |
|
| 316 |
— *end example*]
|
| 317 |
|
| 318 |
+
A name used in the handler for a *function-try-block* [[except.pre]] is
|
| 319 |
+
looked up as if the name was used in the outermost block of the function
|
| 320 |
+
definition. In particular, the function parameter names shall not be
|
| 321 |
+
redeclared in the *exception-declaration* nor in the outermost block of
|
| 322 |
+
a handler for the *function-try-block*. Names declared in the outermost
|
| 323 |
+
block of the function definition are not found when looked up in the
|
| 324 |
+
scope of a handler for the *function-try-block*.
|
| 325 |
|
| 326 |
[*Note 7*: But function parameter names are found. — *end note*]
|
| 327 |
|
| 328 |
[*Note 8*: The rules for name lookup in template definitions are
|
| 329 |
described in [[temp.res]]. — *end note*]
|
| 330 |
|
| 331 |
### Argument-dependent name lookup <a id="basic.lookup.argdep">[[basic.lookup.argdep]]</a>
|
| 332 |
|
| 333 |
+
When the *postfix-expression* in a function call [[expr.call]] is an
|
| 334 |
*unqualified-id*, other namespaces not considered during the usual
|
| 335 |
+
unqualified lookup [[basic.lookup.unqual]] may be searched, and in those
|
| 336 |
+
namespaces, namespace-scope friend function or function template
|
| 337 |
+
declarations [[class.friend]] not otherwise visible may be found. These
|
| 338 |
+
modifications to the search depend on the types of the arguments (and
|
| 339 |
+
for template template arguments, the namespace of the template
|
| 340 |
argument).
|
| 341 |
|
| 342 |
[*Example 1*:
|
| 343 |
|
| 344 |
``` cpp
|
|
|
|
| 356 |
|
| 357 |
— *end example*]
|
| 358 |
|
| 359 |
For each argument type `T` in the function call, there is a set of zero
|
| 360 |
or more *associated namespaces* and a set of zero or more *associated
|
| 361 |
+
entities* (other than namespaces) to be considered. The sets of
|
| 362 |
+
namespaces and entities are determined entirely by the types of the
|
| 363 |
+
function arguments (and the namespace of any template template
|
| 364 |
+
argument). Typedef names and *using-declaration*s used to specify the
|
| 365 |
+
types do not contribute to this set. The sets of namespaces and entities
|
| 366 |
+
are determined in the following way:
|
| 367 |
|
| 368 |
- If `T` is a fundamental type, its associated sets of namespaces and
|
| 369 |
+
entities are both empty.
|
| 370 |
+
- If `T` is a class type (including unions), its associated entities
|
| 371 |
+
are: the class itself; the class of which it is a member, if any; and
|
| 372 |
+
its direct and indirect base classes. Its associated namespaces are
|
| 373 |
+
the innermost enclosing namespaces of its associated entities.
|
| 374 |
+
Furthermore, if `T` is a class template specialization, its associated
|
| 375 |
+
namespaces and entities also include: the namespaces and entities
|
| 376 |
+
associated with the types of the template arguments provided for
|
| 377 |
+
template type parameters (excluding template template parameters); the
|
| 378 |
+
templates used as template template arguments; the namespaces of which
|
| 379 |
+
any template template arguments are members; and the classes of which
|
| 380 |
+
any member templates used as template template arguments are members.
|
| 381 |
+
\[*Note 1*: Non-type template arguments do not contribute to the set
|
| 382 |
+
of associated namespaces. — *end note*]
|
| 383 |
- If `T` is an enumeration type, its associated namespace is the
|
| 384 |
+
innermost enclosing namespace of its declaration, and its associated
|
| 385 |
+
entities are `T` and, if it is a class member, the member’s class.
|
|
|
|
| 386 |
- If `T` is a pointer to `U` or an array of `U`, its associated
|
| 387 |
+
namespaces and entities are those associated with `U`.
|
| 388 |
+
- If `T` is a function type, its associated namespaces and entities are
|
| 389 |
those associated with the function parameter types and those
|
| 390 |
associated with the return type.
|
| 391 |
- If `T` is a pointer to a member function of a class `X`, its
|
| 392 |
+
associated namespaces and entities are those associated with the
|
| 393 |
function parameter types and return type, together with those
|
| 394 |
associated with `X`.
|
| 395 |
- If `T` is a pointer to a data member of class `X`, its associated
|
| 396 |
+
namespaces and entities are those associated with the member type
|
| 397 |
together with those associated with `X`.
|
| 398 |
|
| 399 |
+
If an associated namespace is an inline namespace [[namespace.def]], its
|
| 400 |
+
enclosing namespace is also included in the set. If an associated
|
| 401 |
namespace directly contains inline namespaces, those inline namespaces
|
| 402 |
are also included in the set. In addition, if the argument is the name
|
| 403 |
+
or address of an overload set, its associated entities and namespaces
|
| 404 |
+
are the union of those associated with each of the members of the set,
|
| 405 |
+
i.e., the entities and namespaces associated with its parameter types
|
| 406 |
+
and return type. Additionally, if the aforementioned overload set is
|
| 407 |
+
named with a *template-id*, its associated entities and namespaces also
|
| 408 |
+
include those of its type *template-argument*s and its template
|
| 409 |
+
*template-argument*s.
|
| 410 |
|
| 411 |
+
Let *X* be the lookup set produced by unqualified lookup
|
| 412 |
+
[[basic.lookup.unqual]] and let *Y* be the lookup set produced by
|
| 413 |
argument dependent lookup (defined as follows). If *X* contains
|
| 414 |
|
| 415 |
- a declaration of a class member, or
|
| 416 |
- a block-scope function declaration that is not a *using-declaration*,
|
| 417 |
or
|
|
|
|
| 420 |
then *Y* is empty. Otherwise *Y* is the set of declarations found in the
|
| 421 |
namespaces associated with the argument types as described below. The
|
| 422 |
set of declarations found by the lookup of the name is the union of *X*
|
| 423 |
and *Y*.
|
| 424 |
|
| 425 |
+
[*Note 2*: The namespaces and entities associated with the argument
|
| 426 |
+
types can include namespaces and entities already considered by the
|
| 427 |
ordinary unqualified lookup. — *end note*]
|
| 428 |
|
| 429 |
[*Example 2*:
|
| 430 |
|
| 431 |
``` cpp
|
|
|
|
| 443 |
}
|
| 444 |
```
|
| 445 |
|
| 446 |
— *end example*]
|
| 447 |
|
| 448 |
+
When considering an associated namespace `N`, the lookup is the same as
|
| 449 |
+
the lookup performed when `N` is used as a qualifier [[namespace.qual]]
|
| 450 |
+
except that:
|
| 451 |
|
| 452 |
+
- Any *using-directive*s in `N` are ignored.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 453 |
- All names except those of (possibly overloaded) functions and function
|
| 454 |
templates are ignored.
|
| 455 |
+
- Any namespace-scope friend functions or friend function templates
|
| 456 |
+
[[class.friend]] declared in classes with reachable definitions in the
|
| 457 |
+
set of associated entities are visible within their respective
|
| 458 |
+
namespaces even if they are not visible during an ordinary lookup
|
| 459 |
+
[[namespace.memdef]].
|
| 460 |
+
- Any exported declaration `D` in `N` declared within the purview of a
|
| 461 |
+
named module `M` [[module.interface]] is visible if there is an
|
| 462 |
+
associated entity attached to `M` with the same innermost enclosing
|
| 463 |
+
non-inline namespace as `D`.
|
| 464 |
+
- If the lookup is for a dependent name ([[temp.dep]],
|
| 465 |
+
[[temp.dep.candidate]]), any declaration `D` in `N` is visible if `D`
|
| 466 |
+
would be visible to qualified name lookup [[namespace.qual]] at any
|
| 467 |
+
point in the instantiation context [[module.context]] of the lookup,
|
| 468 |
+
unless `D` is declared in another translation unit, attached to the
|
| 469 |
+
global module, and is either discarded [[module.global.frag]] or has
|
| 470 |
+
internal linkage.
|
| 471 |
+
|
| 472 |
+
[*Example 3*:
|
| 473 |
+
|
| 474 |
+
Translation unit #1
|
| 475 |
+
|
| 476 |
+
``` cpp
|
| 477 |
+
export module M;
|
| 478 |
+
namespace R {
|
| 479 |
+
export struct X {};
|
| 480 |
+
export void f(X);
|
| 481 |
+
}
|
| 482 |
+
namespace S {
|
| 483 |
+
export void f(R::X, R::X);
|
| 484 |
+
}
|
| 485 |
+
```
|
| 486 |
+
|
| 487 |
+
Translation unit #2
|
| 488 |
+
|
| 489 |
+
``` cpp
|
| 490 |
+
export module N;
|
| 491 |
+
import M;
|
| 492 |
+
export R::X make();
|
| 493 |
+
namespace R { static int g(X); }
|
| 494 |
+
export template<typename T, typename U> void apply(T t, U u) {
|
| 495 |
+
f(t, u);
|
| 496 |
+
g(t);
|
| 497 |
+
}
|
| 498 |
+
```
|
| 499 |
+
|
| 500 |
+
Translation unit #3
|
| 501 |
+
|
| 502 |
+
``` cpp
|
| 503 |
+
module Q;
|
| 504 |
+
import N;
|
| 505 |
+
namespace S {
|
| 506 |
+
struct Z { template<typename T> operator T(); };
|
| 507 |
+
}
|
| 508 |
+
void test() {
|
| 509 |
+
auto x = make(); // OK, decltype(x) is R::X in module M
|
| 510 |
+
R::f(x); // error: R and R::f are not visible here
|
| 511 |
+
f(x); // OK, calls R::f from interface of M
|
| 512 |
+
f(x, S::Z()); // error: S::f in module M not considered
|
| 513 |
+
// even though S is an associated namespace
|
| 514 |
+
apply(x, S::Z()); // error: S::f is visible in instantiation context, but
|
| 515 |
+
// R::g has internal linkage and cannot be used outside TU #2
|
| 516 |
+
}
|
| 517 |
+
```
|
| 518 |
+
|
| 519 |
+
— *end example*]
|
| 520 |
|
| 521 |
### Qualified name lookup <a id="basic.lookup.qual">[[basic.lookup.qual]]</a>
|
| 522 |
|
| 523 |
The name of a class or namespace member or enumerator can be referred to
|
| 524 |
+
after the `::` scope resolution operator [[expr.prim.id.qual]] applied
|
| 525 |
+
to a *nested-name-specifier* that denotes its class, namespace, or
|
| 526 |
enumeration. If a `::` scope resolution operator in a
|
| 527 |
*nested-name-specifier* is not preceded by a *decltype-specifier*,
|
| 528 |
lookup of the name preceding that `::` considers only namespaces, types,
|
| 529 |
and templates whose specializations are types. If the name found does
|
| 530 |
not designate a namespace or a class, enumeration, or dependent type,
|
|
|
|
| 538 |
static int n;
|
| 539 |
};
|
| 540 |
int main() {
|
| 541 |
int A;
|
| 542 |
A::n = 42; // OK
|
| 543 |
+
A b; // error: A does not name a type
|
| 544 |
}
|
| 545 |
```
|
| 546 |
|
| 547 |
— *end example*]
|
| 548 |
|
| 549 |
[*Note 1*: Multiply qualified names, such as `N1::N2::N3::n`, can be
|
| 550 |
+
used to refer to members of nested classes [[class.nest]] or members of
|
| 551 |
+
nested namespaces. — *end note*]
|
| 552 |
|
| 553 |
In a declaration in which the *declarator-id* is a *qualified-id*, names
|
| 554 |
used before the *qualified-id* being declared are looked up in the
|
| 555 |
defining namespace scope; names following the *qualified-id* are looked
|
| 556 |
up in the scope of the member’s class or namespace.
|
|
|
|
| 562 |
class C {
|
| 563 |
class X { };
|
| 564 |
static const int number = 50;
|
| 565 |
static X arr[number];
|
| 566 |
};
|
| 567 |
+
X C::arr[number]; // error:
|
| 568 |
// equivalent to ::X C::arr[C::number];
|
| 569 |
// and not to C::X C::arr[C::number];
|
| 570 |
```
|
| 571 |
|
| 572 |
— *end example*]
|
| 573 |
|
| 574 |
+
A name prefixed by the unary scope operator `::` [[expr.prim.id.qual]]
|
| 575 |
+
is looked up in global scope, in the translation unit where it is used.
|
| 576 |
+
The name shall be declared in global namespace scope or shall be a name
|
| 577 |
whose declaration is visible in global scope because of a
|
| 578 |
+
*using-directive* [[namespace.qual]]. The use of `::` allows a global
|
| 579 |
+
name to be referred to even if its identifier has been hidden
|
| 580 |
+
[[basic.scope.hiding]].
|
| 581 |
|
| 582 |
A name prefixed by a *nested-name-specifier* that nominates an
|
| 583 |
enumeration type shall represent an *enumerator* of that enumeration.
|
| 584 |
|
| 585 |
+
In a *qualified-id* of the form:
|
|
|
|
|
|
|
|
|
|
| 586 |
|
| 587 |
``` bnf
|
| 588 |
+
nested-name-specifierₒₚₜ type-name '::' '~' type-name
|
| 589 |
```
|
| 590 |
|
| 591 |
+
the second *type-name* is looked up in the same scope as the first.
|
| 592 |
|
| 593 |
[*Example 3*:
|
| 594 |
|
| 595 |
``` cpp
|
| 596 |
struct C {
|
|
|
|
| 619 |
|
| 620 |
#### Class members <a id="class.qual">[[class.qual]]</a>
|
| 621 |
|
| 622 |
If the *nested-name-specifier* of a *qualified-id* nominates a class,
|
| 623 |
the name specified after the *nested-name-specifier* is looked up in the
|
| 624 |
+
scope of the class [[class.member.lookup]], except for the cases listed
|
| 625 |
+
below. The name shall represent one or more members of that class or of
|
| 626 |
+
one of its base classes [[class.derived]].
|
| 627 |
|
| 628 |
[*Note 1*: A class member can be referred to using a *qualified-id* at
|
| 629 |
+
any point in its potential scope [[basic.scope.class]]. — *end note*]
|
|
|
|
| 630 |
|
| 631 |
The exceptions to the name lookup rule above are the following:
|
| 632 |
|
| 633 |
- the lookup for a destructor is as specified in [[basic.lookup.qual]];
|
| 634 |
- a *conversion-type-id* of a *conversion-function-id* is looked up in
|
| 635 |
the same manner as a *conversion-type-id* in a class member access
|
| 636 |
(see [[basic.lookup.classref]]);
|
| 637 |
- the names in a *template-argument* of a *template-id* are looked up in
|
| 638 |
+
the context in which the entire *postfix-expression* occurs;
|
| 639 |
+
- the lookup for a name specified in a *using-declaration*
|
| 640 |
+
[[namespace.udecl]] also finds class or enumeration names hidden
|
| 641 |
+
within the same scope [[basic.scope.hiding]].
|
| 642 |
|
| 643 |
+
In a lookup in which function names are not ignored[^8] and the
|
| 644 |
*nested-name-specifier* nominates a class `C`:
|
| 645 |
|
| 646 |
- if the name specified after the *nested-name-specifier*, when looked
|
| 647 |
+
up in `C`, is the injected-class-name of `C` [[class.pre]], or
|
| 648 |
+
- in a *using-declarator* of a *using-declaration* [[namespace.udecl]]
|
| 649 |
+
that is a *member-declaration*, if the name specified after the
|
| 650 |
+
*nested-name-specifier* is the same as the *identifier* or the
|
| 651 |
+
*simple-template-id*’s *template-name* in the last component of the
|
| 652 |
+
*nested-name-specifier*,
|
| 653 |
|
| 654 |
the name is instead considered to name the constructor of class `C`.
|
| 655 |
|
| 656 |
[*Note 2*: For example, the constructor is not an acceptable lookup
|
| 657 |
result in an *elaborated-type-specifier* so the constructor would not be
|
|
|
|
| 668 |
|
| 669 |
A::A() { }
|
| 670 |
B::B() { }
|
| 671 |
|
| 672 |
B::A ba; // object of type A
|
| 673 |
+
A::A a; // error: A::A is not a type name
|
| 674 |
struct A::A a2; // object of type A
|
| 675 |
```
|
| 676 |
|
| 677 |
— *end example*]
|
| 678 |
|
|
|
|
| 689 |
names in a *template-argument* of a *template-id* are looked up in the
|
| 690 |
context in which the entire *postfix-expression* occurs.
|
| 691 |
|
| 692 |
For a namespace `X` and name `m`, the namespace-qualified lookup set
|
| 693 |
S(X, m) is defined as follows: Let S'(X, m) be the set of all
|
| 694 |
+
declarations of `m` in `X` and the inline namespace set of `X`
|
| 695 |
+
[[namespace.def]] whose potential scope [[basic.scope.namespace]] would
|
| 696 |
+
include the namespace in which `m` is declared at the location of the
|
| 697 |
+
*nested-name-specifier*. If S'(X, m) is not empty, S(X, m) is S'(X, m);
|
| 698 |
otherwise, S(X, m) is the union of S(Nᵢ, m) for all namespaces Nᵢ
|
| 699 |
nominated by *using-directive*s in `X` and its inline namespace set.
|
| 700 |
|
| 701 |
Given `X::m` (where `X` is a user-declared namespace), or given `::m`
|
| 702 |
(where X is the global namespace), if S(X, m) is the empty set, the
|
| 703 |
program is ill-formed. Otherwise, if S(X, m) has exactly one member, or
|
| 704 |
+
if the context of the reference is a *using-declaration*
|
| 705 |
+
[[namespace.udecl]], S(X, m) is the required set of declarations of `m`.
|
| 706 |
+
Otherwise if the use of `m` is not one that allows a unique declaration
|
| 707 |
+
to be chosen from S(X, m), the program is ill-formed.
|
| 708 |
|
| 709 |
[*Example 1*:
|
| 710 |
|
| 711 |
``` cpp
|
| 712 |
int x;
|
|
|
|
| 842 |
— *end example*]
|
| 843 |
|
| 844 |
During the lookup of a qualified namespace member name, if the lookup
|
| 845 |
finds more than one declaration of the member, and if one declaration
|
| 846 |
introduces a class name or enumeration name and the other declarations
|
| 847 |
+
introduce either the same variable, the same enumerator, or a set of
|
| 848 |
functions, the non-type name hides the class or enumeration name if and
|
| 849 |
only if the declarations are from the same namespace; otherwise (the
|
| 850 |
declarations are from different namespaces), the program is ill-formed.
|
| 851 |
|
| 852 |
[*Example 4*:
|
|
|
|
| 879 |
``` bnf
|
| 880 |
nested-name-specifier unqualified-id
|
| 881 |
```
|
| 882 |
|
| 883 |
the *unqualified-id* shall name a member of the namespace designated by
|
| 884 |
+
the *nested-name-specifier* or of an element of the inline namespace set
|
| 885 |
+
[[namespace.def]] of that namespace.
|
| 886 |
|
| 887 |
[*Example 5*:
|
| 888 |
|
| 889 |
``` cpp
|
| 890 |
namespace A {
|
| 891 |
namespace B {
|
| 892 |
void f1(int);
|
| 893 |
}
|
| 894 |
using namespace B;
|
| 895 |
}
|
| 896 |
+
void A::f1(int){ } // error: f1 is not a member of A
|
| 897 |
```
|
| 898 |
|
| 899 |
— *end example*]
|
| 900 |
|
| 901 |
However, in such namespace member declarations, the
|
|
|
|
| 924 |
|
| 925 |
— *end example*]
|
| 926 |
|
| 927 |
### Elaborated type specifiers <a id="basic.lookup.elab">[[basic.lookup.elab]]</a>
|
| 928 |
|
| 929 |
+
An *elaborated-type-specifier* [[dcl.type.elab]] may be used to refer to
|
| 930 |
+
a previously declared *class-name* or *enum-name* even though the name
|
| 931 |
+
has been hidden by a non-type declaration [[basic.scope.hiding]].
|
|
|
|
| 932 |
|
| 933 |
If the *elaborated-type-specifier* has no *nested-name-specifier*, and
|
| 934 |
unless the *elaborated-type-specifier* appears in a declaration with the
|
| 935 |
following form:
|
| 936 |
|
|
|
|
| 962 |
|
| 963 |
[*Example 1*:
|
| 964 |
|
| 965 |
``` cpp
|
| 966 |
struct Node {
|
| 967 |
+
struct Node* Next; // OK: Refers to injected-class-name Node
|
| 968 |
+
struct Data* Data; // OK: Declares type Data at global scope and member Data
|
|
|
|
| 969 |
};
|
| 970 |
|
| 971 |
struct Data {
|
| 972 |
struct Node* Node; // OK: Refers to Node at global scope
|
| 973 |
+
friend struct ::Glob; // error: Glob is not declared, cannot introduce a qualified type[dcl.type.elab]
|
| 974 |
friend struct Glob; // OK: Refers to (as yet) undeclared Glob at global scope.
|
| 975 |
...
|
| 976 |
};
|
| 977 |
|
| 978 |
struct Base {
|
|
|
|
| 983 |
friend class Data; // OK: nested Data is a friend
|
| 984 |
struct Data { ... }; // Defines nested Data
|
| 985 |
};
|
| 986 |
|
| 987 |
struct Data; // OK: Redeclares Data at global scope
|
| 988 |
+
struct ::Data; // error: cannot introduce a qualified type[dcl.type.elab]
|
| 989 |
+
struct Base::Data; // error: cannot introduce a qualified type[dcl.type.elab]
|
| 990 |
struct Base::Datum; // error: Datum undefined
|
| 991 |
struct Base::Data* pBase; // OK: refers to nested Data
|
| 992 |
```
|
| 993 |
|
| 994 |
— *end example*]
|
| 995 |
|
| 996 |
### Class member access <a id="basic.lookup.classref">[[basic.lookup.classref]]</a>
|
| 997 |
|
| 998 |
+
In a class member access expression [[expr.ref]], if the `.` or `->`
|
| 999 |
token is immediately followed by an *identifier* followed by a `<`, the
|
| 1000 |
identifier must be looked up to determine whether the `<` is the
|
| 1001 |
+
beginning of a template argument list [[temp.names]] or a less-than
|
| 1002 |
operator. The identifier is first looked up in the class of the object
|
| 1003 |
+
expression [[class.member.lookup]]. If the identifier is not found, it
|
| 1004 |
+
is then looked up in the context of the entire *postfix-expression* and
|
| 1005 |
+
shall name a template whose specializations are types.
|
| 1006 |
|
| 1007 |
+
If the *id-expression* in a class member access [[expr.ref]] is an
|
| 1008 |
*unqualified-id*, and the type of the object expression is of a class
|
| 1009 |
+
type `C`, the *unqualified-id* is looked up in the scope of class `C`
|
| 1010 |
+
[[class.member.lookup]].
|
|
|
|
| 1011 |
|
| 1012 |
If the *unqualified-id* is `~`*type-name*, the *type-name* is looked up
|
| 1013 |
in the context of the entire *postfix-expression*. If the type `T` of
|
| 1014 |
the object expression is of a class type `C`, the *type-name* is also
|
| 1015 |
looked up in the scope of class `C`. At least one of the lookups shall
|
|
|
|
| 1037 |
|
| 1038 |
``` cpp
|
| 1039 |
class-name-or-namespace-name::...
|
| 1040 |
```
|
| 1041 |
|
| 1042 |
+
the `class-name-or-namespace-name` following the `.` or `->` operator is
|
| 1043 |
+
first looked up in the class of the object expression
|
| 1044 |
+
[[class.member.lookup]] and the name, if found, is used. Otherwise it is
|
| 1045 |
+
looked up in the context of the entire *postfix-expression*.
|
| 1046 |
|
| 1047 |
[*Note 1*: See [[basic.lookup.qual]], which describes the lookup of a
|
| 1048 |
name before `::`, which will only find a type or namespace
|
| 1049 |
name. — *end note*]
|
| 1050 |
|
|
|
|
| 1052 |
|
| 1053 |
``` cpp
|
| 1054 |
::class-name-or-namespace-name::...
|
| 1055 |
```
|
| 1056 |
|
| 1057 |
+
the `class-name-or-namespace-name` is looked up in global scope as a
|
| 1058 |
*class-name* or *namespace-name*.
|
| 1059 |
|
| 1060 |
+
If the *nested-name-specifier* contains a *simple-template-id*
|
| 1061 |
+
[[temp.names]], the names in its *template-argument*s are looked up in
|
| 1062 |
the context in which the entire *postfix-expression* occurs.
|
| 1063 |
|
| 1064 |
If the *id-expression* is a *conversion-function-id*, its
|
| 1065 |
*conversion-type-id* is first looked up in the class of the object
|
| 1066 |
+
expression [[class.member.lookup]] and the name, if found, is used.
|
| 1067 |
+
Otherwise it is looked up in the context of the entire
|
| 1068 |
+
*postfix-expression*. In each of these lookups, only names that denote
|
| 1069 |
+
types or templates whose specializations are types are considered.
|
| 1070 |
|
| 1071 |
[*Example 2*:
|
| 1072 |
|
| 1073 |
``` cpp
|
| 1074 |
struct A { };
|