- tmp/tmpo5wq8ed_/{from.md → to.md} +470 -176
tmp/tmpo5wq8ed_/{from.md → to.md}
RENAMED
|
@@ -2,11 +2,11 @@
|
|
| 2 |
|
| 3 |
Three kinds of names can be used within a template definition:
|
| 4 |
|
| 5 |
- The name of the template itself, and names declared within the
|
| 6 |
template itself.
|
| 7 |
-
- Names dependent on a *template-parameter*
|
| 8 |
- Names from scopes which are visible within the template definition.
|
| 9 |
|
| 10 |
A name used in a template declaration or definition and that is
|
| 11 |
dependent on a *template-parameter* is assumed not to name a type unless
|
| 12 |
the applicable name lookup finds a type name or the name is qualified by
|
|
@@ -28,38 +28,31 @@ template<class T> class Y {
|
|
| 28 |
Y* a3; // declare pointer to Y<T>
|
| 29 |
Z* a4; // declare pointer to Z
|
| 30 |
typedef typename T::A TA;
|
| 31 |
TA* a5; // declare pointer to T's A
|
| 32 |
typename T::A* a6; // declare pointer to T's A
|
| 33 |
-
T::A* a7; //
|
| 34 |
-
//
|
| 35 |
-
B* a8; //
|
| 36 |
-
//
|
| 37 |
}
|
| 38 |
};
|
| 39 |
```
|
| 40 |
|
| 41 |
— *end example*]
|
| 42 |
|
| 43 |
-
When a *qualified-id* is intended to refer to a type that is not a
|
| 44 |
-
member of the current instantiation ([[temp.dep.type]]) and its
|
| 45 |
-
*nested-name-specifier* refers to a dependent type, it shall be prefixed
|
| 46 |
-
by the keyword `typename`, forming a *typename-specifier*. If the
|
| 47 |
-
*qualified-id* in a *typename-specifier* does not denote a type or a
|
| 48 |
-
class template, the program is ill-formed.
|
| 49 |
-
|
| 50 |
``` bnf
|
| 51 |
typename-specifier:
|
| 52 |
-
|
| 53 |
-
|
| 54 |
```
|
| 55 |
|
| 56 |
-
|
| 57 |
-
*
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
|
| 62 |
[*Example 2*:
|
| 63 |
|
| 64 |
``` cpp
|
| 65 |
struct A {
|
|
@@ -80,33 +73,75 @@ void foo() {
|
|
| 80 |
}
|
| 81 |
```
|
| 82 |
|
| 83 |
— *end example*]
|
| 84 |
|
| 85 |
-
A qualified name used as the name in a *class-or-decltype*
|
| 86 |
-
[[class.derived]]
|
| 87 |
assumed to name a type, without the use of the `typename` keyword. In a
|
| 88 |
*nested-name-specifier* that immediately contains a
|
| 89 |
*nested-name-specifier* that depends on a template parameter, the
|
| 90 |
*identifier* or *simple-template-id* is implicitly assumed to name a
|
| 91 |
type, without the use of the `typename` keyword.
|
| 92 |
|
| 93 |
[*Note 1*: The `typename` keyword is not permitted by the syntax of
|
| 94 |
these constructs. — *end note*]
|
| 95 |
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
|
| 103 |
[*Example 3*:
|
| 104 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
``` cpp
|
| 106 |
template <class T> void f(int i) {
|
| 107 |
-
T::x * i; //
|
| 108 |
}
|
| 109 |
|
| 110 |
struct Foo {
|
| 111 |
typedef int x;
|
| 112 |
};
|
|
@@ -123,38 +158,36 @@ int main() {
|
|
| 123 |
|
| 124 |
— *end example*]
|
| 125 |
|
| 126 |
Within the definition of a class template or within the definition of a
|
| 127 |
member of a class template following the *declarator-id*, the keyword
|
| 128 |
-
`typename` is not required when referring to
|
| 129 |
-
|
| 130 |
-
template.
|
| 131 |
|
| 132 |
-
[*
|
| 133 |
-
[[basic.lookup.unqual]]), class member lookup ([[class.qual]]) into the
|
| 134 |
-
current instantiation ([[temp.dep.type]]), or class member access
|
| 135 |
-
expression lookup ([[basic.lookup.classref]]) when the type of the
|
| 136 |
-
object expression is the current instantiation (
|
| 137 |
-
[[temp.dep.expr]]). — *end note*]
|
| 138 |
-
|
| 139 |
-
[*Example 4*:
|
| 140 |
|
| 141 |
``` cpp
|
| 142 |
template<class T> struct A {
|
| 143 |
typedef int B;
|
| 144 |
B b; // OK, no typename required
|
| 145 |
};
|
| 146 |
```
|
| 147 |
|
| 148 |
— *end example*]
|
| 149 |
|
| 150 |
-
|
| 151 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 152 |
|
| 153 |
- no valid specialization can be generated for a template or a
|
| 154 |
-
substatement of a constexpr if statement
|
| 155 |
-
|
|
|
|
|
|
|
| 156 |
- every valid specialization of a variadic template requires an empty
|
| 157 |
template parameter pack, or
|
| 158 |
- a hypothetical instantiation of a template immediately following its
|
| 159 |
definition would be ill-formed due to a construct that does not depend
|
| 160 |
on a template parameter, or
|
|
@@ -172,13 +205,13 @@ to be checked. The program is ill-formed, no diagnostic required, if:
|
|
| 172 |
*using-declaration* was a pack expansion and the corresponding pack
|
| 173 |
is empty, or
|
| 174 |
- an instantiation uses a default argument or default template
|
| 175 |
argument that had not been defined at the point at which the
|
| 176 |
template was defined, or
|
| 177 |
-
- constant expression evaluation
|
| 178 |
instantiation uses
|
| 179 |
-
- the value of a
|
| 180 |
type or
|
| 181 |
- the value of a `constexpr` object or
|
| 182 |
- the value of a reference or
|
| 183 |
- the definition of a constexpr function,
|
| 184 |
|
|
@@ -194,15 +227,14 @@ to be checked. The program is ill-formed, no diagnostic required, if:
|
|
| 194 |
|
| 195 |
Otherwise, no diagnostic shall be issued for a template for which a
|
| 196 |
valid specialization can be generated.
|
| 197 |
|
| 198 |
[*Note 4*: If a template is instantiated, errors will be diagnosed
|
| 199 |
-
according to the other rules in this
|
| 200 |
-
|
| 201 |
-
issue. — *end note*]
|
| 202 |
|
| 203 |
-
[*Example
|
| 204 |
|
| 205 |
``` cpp
|
| 206 |
int j;
|
| 207 |
template<class T> class X {
|
| 208 |
void f(T t, int i, char* p) {
|
|
@@ -226,13 +258,13 @@ template<class... T> struct A : T..., T... { }; // error: duplicate base cla
|
|
| 226 |
|
| 227 |
When looking for the declaration of a name used in a template
|
| 228 |
definition, the usual lookup rules ([[basic.lookup.unqual]],
|
| 229 |
[[basic.lookup.argdep]]) are used for non-dependent names. The lookup of
|
| 230 |
names dependent on the template parameters is postponed until the actual
|
| 231 |
-
template argument is known
|
| 232 |
|
| 233 |
-
[*Example
|
| 234 |
|
| 235 |
``` cpp
|
| 236 |
#include <iostream>
|
| 237 |
using namespace std;
|
| 238 |
|
|
@@ -247,30 +279,30 @@ public:
|
|
| 247 |
cout << p[i] << '\n';
|
| 248 |
}
|
| 249 |
};
|
| 250 |
```
|
| 251 |
|
| 252 |
-
|
| 253 |
`cnt` is the member `cnt` declared in `Set`, and `cout` is the standard
|
| 254 |
output stream declared in `iostream`. However, not every declaration can
|
| 255 |
be found this way; the resolution of some names must be postponed until
|
| 256 |
the actual *template-argument*s are known. For example, even though the
|
| 257 |
name `operator<<` is known within the definition of `printall()` and a
|
| 258 |
declaration of it can be found in `<iostream>`, the actual declaration
|
| 259 |
of `operator<<` needed to print `p[i]` cannot be known until it is known
|
| 260 |
-
what type `T` is
|
| 261 |
|
| 262 |
— *end example*]
|
| 263 |
|
| 264 |
If a name does not depend on a *template-parameter* (as defined in
|
| 265 |
[[temp.dep]]), a declaration (or set of declarations) for that name
|
| 266 |
shall be in scope at the point where the name appears in the template
|
| 267 |
definition; the name is bound to the declaration (or declarations) found
|
| 268 |
at that point and this binding is not affected by declarations that are
|
| 269 |
visible at the point of instantiation.
|
| 270 |
|
| 271 |
-
[*Example
|
| 272 |
|
| 273 |
``` cpp
|
| 274 |
void f(char);
|
| 275 |
|
| 276 |
template<class T> void g(T t) {
|
|
@@ -293,23 +325,24 @@ void h() {
|
|
| 293 |
— *end example*]
|
| 294 |
|
| 295 |
[*Note 5*: For purposes of name lookup, default arguments and
|
| 296 |
*noexcept-specifier*s of function templates and default arguments and
|
| 297 |
*noexcept-specifier*s of member functions of class templates are
|
| 298 |
-
considered definitions
|
| 299 |
|
| 300 |
### Locally declared names <a id="temp.local">[[temp.local]]</a>
|
| 301 |
|
| 302 |
Like normal (non-template) classes, class templates have an
|
| 303 |
-
injected-class-name
|
| 304 |
-
|
| 305 |
*template-argument-list*, as a *template-argument* for a template
|
| 306 |
*template-parameter*, or as the final identifier in the
|
| 307 |
*elaborated-type-specifier* of a friend class template declaration, it
|
| 308 |
-
refers to the class template itself.
|
| 309 |
-
*
|
| 310 |
-
template enclosed in
|
|
|
|
| 311 |
|
| 312 |
Within the scope of a class template specialization or partial
|
| 313 |
specialization, when the injected-class-name is used as a *type-name*,
|
| 314 |
it is equivalent to the *template-name* followed by the
|
| 315 |
*template-argument*s of the class template specialization or partial
|
|
@@ -331,11 +364,11 @@ template<> class Y<int> {
|
|
| 331 |
```
|
| 332 |
|
| 333 |
— *end example*]
|
| 334 |
|
| 335 |
The injected-class-name of a class template or class template
|
| 336 |
-
specialization can be used
|
| 337 |
wherever it is in scope.
|
| 338 |
|
| 339 |
[*Example 2*:
|
| 340 |
|
| 341 |
``` cpp
|
|
@@ -346,20 +379,20 @@ template <class T> struct Base {
|
|
| 346 |
template <class T> struct Derived: public Base<T> {
|
| 347 |
typename Derived::Base* p; // meaning Derived::Base<T>
|
| 348 |
};
|
| 349 |
|
| 350 |
template<class T, template<class> class U = T::template Base> struct Third { };
|
| 351 |
-
Third<
|
| 352 |
```
|
| 353 |
|
| 354 |
— *end example*]
|
| 355 |
|
| 356 |
-
A lookup that finds an injected-class-name
|
| 357 |
-
|
| 358 |
-
|
| 359 |
-
|
| 360 |
-
|
| 361 |
template itself and not a specialization thereof, and is not ambiguous.
|
| 362 |
|
| 363 |
[*Example 3*:
|
| 364 |
|
| 365 |
``` cpp
|
|
@@ -388,13 +421,13 @@ template<class T> class X {
|
|
| 388 |
};
|
| 389 |
```
|
| 390 |
|
| 391 |
— *end example*]
|
| 392 |
|
| 393 |
-
|
| 394 |
-
(including nested scopes). A *template-parameter* shall not have
|
| 395 |
-
same name as the template name.
|
| 396 |
|
| 397 |
[*Example 5*:
|
| 398 |
|
| 399 |
``` cpp
|
| 400 |
template<class T, int i> class Y {
|
|
@@ -457,14 +490,14 @@ template<class C> void N::B<C>::f(C) {
|
|
| 457 |
|
| 458 |
— *end example*]
|
| 459 |
|
| 460 |
In the definition of a class template or in the definition of a member
|
| 461 |
of such a template that appears outside of the template definition, for
|
| 462 |
-
each non-dependent base class
|
| 463 |
-
|
| 464 |
-
|
| 465 |
-
|
| 466 |
|
| 467 |
[*Example 8*:
|
| 468 |
|
| 469 |
``` cpp
|
| 470 |
struct A {
|
|
@@ -486,31 +519,38 @@ template<class B, class a> struct X : A {
|
|
| 486 |
Inside a template, some constructs have semantics which may differ from
|
| 487 |
one instantiation to another. Such a construct *depends* on the template
|
| 488 |
parameters. In particular, types and expressions may depend on the type
|
| 489 |
and/or value of template parameters (as determined by the template
|
| 490 |
arguments) and this determines the context for name lookup for certain
|
| 491 |
-
names. An
|
| 492 |
depend on a template parameter) or *value-dependent* (that is, its value
|
| 493 |
-
when evaluated as a constant expression
|
| 494 |
-
|
| 495 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 496 |
|
| 497 |
where the *postfix-expression* is an *unqualified-id*, the
|
| 498 |
*unqualified-id* denotes a *dependent name* if
|
| 499 |
|
| 500 |
-
- any of the expressions in the *expression-list* is a pack expansion
|
| 501 |
-
[[temp.variadic]]
|
| 502 |
- any of the expressions or *braced-init-list*s in the *expression-list*
|
| 503 |
-
is type-dependent
|
| 504 |
- the *unqualified-id* is a *template-id* in which any of the template
|
| 505 |
arguments depends on a template parameter.
|
| 506 |
|
| 507 |
If an operand of an operator is a type-dependent expression, the
|
| 508 |
-
operator also denotes a dependent name.
|
| 509 |
-
|
| 510 |
-
|
| 511 |
-
point of
|
|
|
|
|
|
|
| 512 |
|
| 513 |
[*Example 1*:
|
| 514 |
|
| 515 |
``` cpp
|
| 516 |
template<class T> struct X : B<T> {
|
|
@@ -520,17 +560,17 @@ template<class T> struct X : B<T> {
|
|
| 520 |
pb->j++;
|
| 521 |
}
|
| 522 |
};
|
| 523 |
```
|
| 524 |
|
| 525 |
-
|
| 526 |
and `pb->j` explicitly depend on the *template-parameter*.
|
| 527 |
|
| 528 |
— *end example*]
|
| 529 |
|
| 530 |
In the definition of a class or class template, the scope of a dependent
|
| 531 |
-
base class
|
| 532 |
lookup either at the point of definition of the class template or member
|
| 533 |
or during an instantiation of the class template or member.
|
| 534 |
|
| 535 |
[*Example 2*:
|
| 536 |
|
|
@@ -580,41 +620,50 @@ not affect the binding of names in `Y<A>`.
|
|
| 580 |
|
| 581 |
A name refers to the *current instantiation* if it is
|
| 582 |
|
| 583 |
- in the definition of a class template, a nested class of a class
|
| 584 |
template, a member of a class template, or a member of a nested class
|
| 585 |
-
of a class template, the injected-class-name
|
| 586 |
-
|
| 587 |
- in the definition of a primary class template or a member of a primary
|
| 588 |
class template, the name of the class template followed by the
|
| 589 |
template argument list of the primary template (as described below)
|
| 590 |
enclosed in `<>` (or an equivalent template alias specialization),
|
| 591 |
- in the definition of a nested class of a class template, the name of
|
| 592 |
the nested class referenced as a member of the current instantiation,
|
| 593 |
or
|
| 594 |
- in the definition of a partial specialization or a member of a partial
|
| 595 |
specialization, the name of the class template followed by the
|
| 596 |
template argument list of the partial specialization enclosed in `<>`
|
| 597 |
-
(or an equivalent template alias specialization). If the
|
| 598 |
-
|
| 599 |
-
|
| 600 |
-
parameter pack.
|
| 601 |
|
| 602 |
The template argument list of a primary template is a template argument
|
| 603 |
-
list in which the
|
| 604 |
-
template parameter of the class template. If the
|
| 605 |
-
|
| 606 |
-
|
| 607 |
-
|
| 608 |
|
| 609 |
-
A template argument that is equivalent to a template parameter
|
| 610 |
-
|
| 611 |
-
|
| 612 |
-
|
| 613 |
-
|
| 614 |
-
|
| 615 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 616 |
|
| 617 |
[*Example 1*:
|
| 618 |
|
| 619 |
``` cpp
|
| 620 |
template <class T> class A {
|
|
@@ -639,22 +688,26 @@ template <class T1, class T2, int I> struct B {
|
|
| 639 |
B<T2, T1, I>* b2; // not the current instantiation
|
| 640 |
typedef T1 my_T1;
|
| 641 |
static const int my_I = I;
|
| 642 |
static const int my_I2 = I+0;
|
| 643 |
static const int my_I3 = my_I;
|
|
|
|
|
|
|
| 644 |
B<my_T1, T2, my_I>* b3; // refers to the current instantiation
|
| 645 |
B<my_T1, T2, my_I2>* b4; // not the current instantiation
|
| 646 |
B<my_T1, T2, my_I3>* b5; // refers to the current instantiation
|
|
|
|
|
|
|
| 647 |
};
|
| 648 |
```
|
| 649 |
|
| 650 |
— *end example*]
|
| 651 |
|
| 652 |
A *dependent base class* is a base class that is a dependent type and is
|
| 653 |
not the current instantiation.
|
| 654 |
|
| 655 |
-
[*Note
|
| 656 |
|
| 657 |
A base class can be the current instantiation in the case of a nested
|
| 658 |
class naming an enclosing class as a base.
|
| 659 |
|
| 660 |
[*Example 2*:
|
|
@@ -679,26 +732,26 @@ template<class T> struct A<T>::B::C : A<T> {
|
|
| 679 |
|
| 680 |
A name is a *member of the current instantiation* if it is
|
| 681 |
|
| 682 |
- An unqualified name that, when looked up, refers to at least one
|
| 683 |
member of a class that is the current instantiation or a non-dependent
|
| 684 |
-
base class thereof. \[*Note
|
| 685 |
name in a scope enclosed by the definition of a class
|
| 686 |
template. — *end note*]
|
| 687 |
- A *qualified-id* in which the *nested-name-specifier* refers to the
|
| 688 |
current instantiation and that, when looked up, refers to at least one
|
| 689 |
member of a class that is the current instantiation or a non-dependent
|
| 690 |
-
base class thereof. \[*Note
|
| 691 |
current instantiation has any dependent base classes, then the
|
| 692 |
*qualified-id* is a member of an unknown specialization; see
|
| 693 |
below. — *end note*]
|
| 694 |
- An *id-expression* denoting the member in a class member access
|
| 695 |
-
expression
|
| 696 |
-
|
| 697 |
-
|
| 698 |
-
|
| 699 |
-
thereof. \[*Note
|
| 700 |
instantiation has any dependent base classes, then the *id-expression*
|
| 701 |
is a member of an unknown specialization; see below. — *end note*]
|
| 702 |
|
| 703 |
[*Example 3*:
|
| 704 |
|
|
@@ -730,18 +783,18 @@ A name is a *member of an unknown specialization* if it is
|
|
| 730 |
current instantiation, the current instantiation has at least one
|
| 731 |
dependent base class, and name lookup of the *qualified-id* does not
|
| 732 |
find any member of a class that is the current instantiation or a
|
| 733 |
non-dependent base class thereof.
|
| 734 |
- An *id-expression* denoting the member in a class member access
|
| 735 |
-
expression
|
| 736 |
- the type of the object expression is the current instantiation, the
|
| 737 |
current instantiation has at least one dependent base class, and
|
| 738 |
name lookup of the *id-expression* does not find a member of a class
|
| 739 |
that is the current instantiation or a non-dependent base class
|
| 740 |
thereof; or
|
| 741 |
-
- the type of the object expression is
|
| 742 |
-
|
| 743 |
|
| 744 |
If a *qualified-id* in which the *nested-name-specifier* refers to the
|
| 745 |
current instantiation is not a member of the current instantiation or a
|
| 746 |
member of an unknown specialization, the program is ill-formed even if
|
| 747 |
the template containing the *qualified-id* is not instantiated; no
|
|
@@ -808,20 +861,20 @@ A type is dependent if it is
|
|
| 808 |
- a cv-qualified type where the cv-unqualified type is dependent,
|
| 809 |
- a compound type constructed from any dependent type,
|
| 810 |
- an array type whose element type is dependent or whose bound (if any)
|
| 811 |
is value-dependent,
|
| 812 |
- a function type whose exception specification is value-dependent,
|
| 813 |
-
- a *simple-template-id* in which either the template name is
|
| 814 |
-
parameter or any of the template arguments is a dependent
|
| 815 |
-
expression that is type-dependent or value-dependent or is
|
| 816 |
-
expansion \[*Note
|
| 817 |
-
[[class]]
|
| 818 |
*template-argument-list*. — *end note*] , or
|
| 819 |
- denoted by `decltype(`*expression*`)`, where *expression* is
|
| 820 |
-
type-dependent
|
| 821 |
|
| 822 |
-
[*Note
|
| 823 |
simply refer to other types, a name that refers to a typedef that is a
|
| 824 |
member of the current instantiation is dependent only if the type
|
| 825 |
referred to is dependent. — *end note*]
|
| 826 |
|
| 827 |
#### Type-dependent expressions <a id="temp.dep.expr">[[temp.dep.expr]]</a>
|
|
@@ -830,49 +883,77 @@ Except as described below, an expression is type-dependent if any
|
|
| 830 |
subexpression is type-dependent.
|
| 831 |
|
| 832 |
`this`
|
| 833 |
|
| 834 |
is type-dependent if the class type of the enclosing member function is
|
| 835 |
-
dependent
|
| 836 |
|
| 837 |
-
An *id-expression* is type-dependent if it
|
|
|
|
| 838 |
|
| 839 |
- an *identifier* associated by name lookup with one or more
|
| 840 |
declarations declared with a dependent type,
|
| 841 |
- an *identifier* associated by name lookup with a non-type
|
| 842 |
*template-parameter* declared with a type that contains a placeholder
|
| 843 |
-
type
|
|
|
|
|
|
|
|
|
|
| 844 |
- an *identifier* associated by name lookup with one or more
|
| 845 |
declarations of member functions of the current instantiation declared
|
| 846 |
with a return type that contains a placeholder type,
|
| 847 |
- an *identifier* associated by name lookup with a structured binding
|
| 848 |
-
declaration
|
| 849 |
-
|
| 850 |
-
- the *identifier* `__func__`
|
| 851 |
enclosing function is a template, a member of a class template, or a
|
| 852 |
generic lambda,
|
| 853 |
- a *template-id* that is dependent,
|
| 854 |
- a *conversion-function-id* that specifies a dependent type, or
|
| 855 |
- a *nested-name-specifier* or a *qualified-id* that names a member of
|
| 856 |
an unknown specialization;
|
| 857 |
|
| 858 |
or if it names a dependent member of the current instantiation that is a
|
| 859 |
-
static data member of type “array of unknown bound of `T`” for some
|
| 860 |
-
|
| 861 |
-
|
| 862 |
-
*
|
| 863 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 864 |
|
| 865 |
Expressions of the following forms are never type-dependent (because the
|
| 866 |
type of the expression cannot be dependent):
|
| 867 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 868 |
[*Note 1*: For the standard library macro `offsetof`, see
|
| 869 |
[[support.types]]. — *end note*]
|
| 870 |
|
| 871 |
-
A class member access expression
|
| 872 |
-
|
| 873 |
-
|
| 874 |
expression refers to a member of an unknown specialization.
|
| 875 |
|
| 876 |
[*Note 2*: In an expression of the form `x.y` or `xp->y` the type of
|
| 877 |
the expression is usually the type of the member `y` of the class of `x`
|
| 878 |
(or the class pointed to by `xp`). However, if `x` or `xp` refers to a
|
|
@@ -892,37 +973,60 @@ Except as described below, an expression used in a context where a
|
|
| 892 |
constant expression is required is value-dependent if any subexpression
|
| 893 |
is value-dependent.
|
| 894 |
|
| 895 |
An *id-expression* is value-dependent if:
|
| 896 |
|
|
|
|
| 897 |
- it is type-dependent,
|
| 898 |
- it is the name of a non-type template parameter,
|
| 899 |
- it names a static data member that is a dependent member of the
|
| 900 |
current instantiation and is not initialized in a *member-declarator*,
|
| 901 |
- it names a static member function that is a dependent member of the
|
| 902 |
current instantiation, or
|
| 903 |
-
- it
|
| 904 |
-
expression that is value-dependent.
|
| 905 |
|
| 906 |
Expressions of the following form are value-dependent if the
|
| 907 |
*unary-expression* or *expression* is type-dependent or the *type-id* is
|
| 908 |
dependent:
|
| 909 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 910 |
[*Note 1*: For the standard library macro `offsetof`, see
|
| 911 |
[[support.types]]. — *end note*]
|
| 912 |
|
| 913 |
Expressions of the following form are value-dependent if either the
|
| 914 |
*type-id* or *simple-type-specifier* is dependent or the *expression* or
|
| 915 |
*cast-expression* is value-dependent:
|
| 916 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 917 |
Expressions of the following form are value-dependent:
|
| 918 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 919 |
An expression of the form `&`*qualified-id* where the *qualified-id*
|
| 920 |
names a dependent member of the current instantiation is
|
| 921 |
value-dependent. An expression of the form `&`*cast-expression* is also
|
| 922 |
value-dependent if evaluating *cast-expression* as a core constant
|
| 923 |
-
expression
|
| 924 |
refers to a templated entity that is an object with static or thread
|
| 925 |
storage duration or a member function.
|
| 926 |
|
| 927 |
#### Dependent template arguments <a id="temp.dep.temp">[[temp.dep.temp]]</a>
|
| 928 |
|
|
@@ -966,19 +1070,10 @@ void g(int); // not in scope at the point of the template definition, not
|
|
| 966 |
|
| 967 |
— *end example*]
|
| 968 |
|
| 969 |
### Dependent name resolution <a id="temp.dep.res">[[temp.dep.res]]</a>
|
| 970 |
|
| 971 |
-
In resolving dependent names, names from the following sources are
|
| 972 |
-
considered:
|
| 973 |
-
|
| 974 |
-
- Declarations that are visible at the point of definition of the
|
| 975 |
-
template.
|
| 976 |
-
- Declarations from namespaces associated with the types of the function
|
| 977 |
-
arguments both from the instantiation context ([[temp.point]]) and
|
| 978 |
-
from the definition context.
|
| 979 |
-
|
| 980 |
#### Point of instantiation <a id="temp.point">[[temp.point]]</a>
|
| 981 |
|
| 982 |
For a function template specialization, a member function template
|
| 983 |
specialization, or a specialization for a member function or static data
|
| 984 |
member of a class template, if the specialization is implicitly
|
|
@@ -1024,61 +1119,260 @@ enclosing class template specialization.
|
|
| 1024 |
|
| 1025 |
An explicit instantiation definition is an instantiation point for the
|
| 1026 |
specialization or specializations specified by the explicit
|
| 1027 |
instantiation.
|
| 1028 |
|
| 1029 |
-
The instantiation context of an expression that depends on the template
|
| 1030 |
-
arguments is the set of declarations with external linkage declared
|
| 1031 |
-
prior to the point of instantiation of the template specialization in
|
| 1032 |
-
the same translation unit.
|
| 1033 |
-
|
| 1034 |
A specialization for a function template, a member function template, or
|
| 1035 |
of a member function or static data member of a class template may have
|
| 1036 |
multiple points of instantiations within a translation unit, and in
|
| 1037 |
-
addition to the points of instantiation described above,
|
| 1038 |
-
|
| 1039 |
-
|
| 1040 |
-
|
| 1041 |
-
|
| 1042 |
-
|
| 1043 |
-
|
| 1044 |
-
specialization
|
| 1045 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1046 |
|
| 1047 |
#### Candidate functions <a id="temp.dep.candidate">[[temp.dep.candidate]]</a>
|
| 1048 |
|
| 1049 |
For a function call where the *postfix-expression* is a dependent name,
|
| 1050 |
-
the candidate functions are found using the usual lookup rules
|
| 1051 |
-
[[basic.lookup.unqual]],
|
|
|
|
| 1052 |
|
| 1053 |
-
|
| 1054 |
-
|
| 1055 |
-
|
| 1056 |
-
|
| 1057 |
-
[[basic.lookup.argdep]]), only function declarations found in either
|
| 1058 |
-
the template definition context or the template instantiation context
|
| 1059 |
-
are found.
|
| 1060 |
|
| 1061 |
If the call would be ill-formed or would find a better match had the
|
| 1062 |
lookup within the associated namespaces considered all the function
|
| 1063 |
declarations with external linkage introduced in those namespaces in all
|
| 1064 |
translation units, not just considering those declarations found in the
|
| 1065 |
template definition and template instantiation contexts, then the
|
| 1066 |
program has undefined behavior.
|
| 1067 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1068 |
### Friend names declared within a class template <a id="temp.inject">[[temp.inject]]</a>
|
| 1069 |
|
| 1070 |
Friend classes or functions can be declared within a class template.
|
| 1071 |
When a template is instantiated, the names of its friends are treated as
|
| 1072 |
if the specialization had been explicitly declared at its point of
|
| 1073 |
instantiation.
|
| 1074 |
|
| 1075 |
As with non-template classes, the names of namespace-scope friend
|
| 1076 |
functions of a class template specialization are not visible during an
|
| 1077 |
-
ordinary lookup unless explicitly declared at namespace scope
|
| 1078 |
-
[[class.friend]]
|
| 1079 |
-
|
| 1080 |
|
| 1081 |
[*Example 1*:
|
| 1082 |
|
| 1083 |
``` cpp
|
| 1084 |
template<typename T> struct number {
|
|
@@ -1088,11 +1382,11 @@ template<typename T> struct number {
|
|
| 1088 |
|
| 1089 |
void g() {
|
| 1090 |
number<double> a(3), b(4);
|
| 1091 |
a = gcd(a,b); // finds gcd because number<double> is an associated class,
|
| 1092 |
// making gcd visible in its namespace (global scope)
|
| 1093 |
-
b = gcd(3,4); //
|
| 1094 |
}
|
| 1095 |
```
|
| 1096 |
|
| 1097 |
— *end example*]
|
| 1098 |
|
|
|
|
| 2 |
|
| 3 |
Three kinds of names can be used within a template definition:
|
| 4 |
|
| 5 |
- The name of the template itself, and names declared within the
|
| 6 |
template itself.
|
| 7 |
+
- Names dependent on a *template-parameter* [[temp.dep]].
|
| 8 |
- Names from scopes which are visible within the template definition.
|
| 9 |
|
| 10 |
A name used in a template declaration or definition and that is
|
| 11 |
dependent on a *template-parameter* is assumed not to name a type unless
|
| 12 |
the applicable name lookup finds a type name or the name is qualified by
|
|
|
|
| 28 |
Y* a3; // declare pointer to Y<T>
|
| 29 |
Z* a4; // declare pointer to Z
|
| 30 |
typedef typename T::A TA;
|
| 31 |
TA* a5; // declare pointer to T's A
|
| 32 |
typename T::A* a6; // declare pointer to T's A
|
| 33 |
+
T::A* a7; // error: no visible declaration of a7
|
| 34 |
+
// T::A is not a type name; multiplication of T::A by a7
|
| 35 |
+
B* a8; // error: no visible declarations of B and a8
|
| 36 |
+
// B is not a type name; multiplication of B by a8
|
| 37 |
}
|
| 38 |
};
|
| 39 |
```
|
| 40 |
|
| 41 |
— *end example*]
|
| 42 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
``` bnf
|
| 44 |
typename-specifier:
|
| 45 |
+
typename nested-name-specifier identifier
|
| 46 |
+
typename nested-name-specifier 'templateₒₚₜ ' simple-template-id
|
| 47 |
```
|
| 48 |
|
| 49 |
+
A *typename-specifier* denotes the type or class template denoted by the
|
| 50 |
+
*simple-type-specifier* [[dcl.type.simple]] formed by omitting the
|
| 51 |
+
keyword `typename`. The usual qualified name lookup
|
| 52 |
+
[[basic.lookup.qual]] is used to find the *qualified-id* even in the
|
| 53 |
+
presence of `typename`.
|
| 54 |
|
| 55 |
[*Example 2*:
|
| 56 |
|
| 57 |
``` cpp
|
| 58 |
struct A {
|
|
|
|
| 73 |
}
|
| 74 |
```
|
| 75 |
|
| 76 |
— *end example*]
|
| 77 |
|
| 78 |
+
A qualified name used as the name in a *class-or-decltype*
|
| 79 |
+
[[class.derived]] or an *elaborated-type-specifier* is implicitly
|
| 80 |
assumed to name a type, without the use of the `typename` keyword. In a
|
| 81 |
*nested-name-specifier* that immediately contains a
|
| 82 |
*nested-name-specifier* that depends on a template parameter, the
|
| 83 |
*identifier* or *simple-template-id* is implicitly assumed to name a
|
| 84 |
type, without the use of the `typename` keyword.
|
| 85 |
|
| 86 |
[*Note 1*: The `typename` keyword is not permitted by the syntax of
|
| 87 |
these constructs. — *end note*]
|
| 88 |
|
| 89 |
+
A *qualified-id* is assumed to name a type if
|
| 90 |
+
|
| 91 |
+
- it is a qualified name in a type-id-only context (see below), or
|
| 92 |
+
- it is a *decl-specifier* of the *decl-specifier-seq* of a
|
| 93 |
+
- *simple-declaration* or a *function-definition* in namespace scope,
|
| 94 |
+
- *member-declaration*,
|
| 95 |
+
- *parameter-declaration* in a *member-declaration* [^10], unless that
|
| 96 |
+
*parameter-declaration* appears in a default argument,
|
| 97 |
+
- *parameter-declaration* in a *declarator* of a function or function
|
| 98 |
+
template declaration whose *declarator-id* is qualified, unless that
|
| 99 |
+
*parameter-declaration* appears in a default argument,
|
| 100 |
+
- *parameter-declaration* in a *lambda-declarator* or
|
| 101 |
+
*requirement-parameter-list*, unless that *parameter-declaration*
|
| 102 |
+
appears in a default argument, or
|
| 103 |
+
- *parameter-declaration* of a (non-type) *template-parameter*.
|
| 104 |
+
|
| 105 |
+
A qualified name is said to be in a *type-id-only context* if it appears
|
| 106 |
+
in a *type-id*, *new-type-id*, or *defining-type-id* and the smallest
|
| 107 |
+
enclosing *type-id*, *new-type-id*, or *defining-type-id* is a
|
| 108 |
+
*new-type-id*, *defining-type-id*, *trailing-return-type*, default
|
| 109 |
+
argument of a *type-parameter* of a template, or *type-id* of a
|
| 110 |
+
`static_cast`, `const_cast`, `reinterpret_cast`, or `dynamic_cast`.
|
| 111 |
|
| 112 |
[*Example 3*:
|
| 113 |
|
| 114 |
+
``` cpp
|
| 115 |
+
template<class T> T::R f(); // OK, return type of a function declaration at global scope
|
| 116 |
+
template<class T> void f(T::R); // ill-formed, no diagnostic required: attempt to declare
|
| 117 |
+
// a void variable template
|
| 118 |
+
template<class T> struct S {
|
| 119 |
+
using Ptr = PtrTraits<T>::Ptr; // OK, in a defining-type-id
|
| 120 |
+
T::R f(T::P p) { // OK, class scope
|
| 121 |
+
return static_cast<T::R>(p); // OK, type-id of a static_cast
|
| 122 |
+
}
|
| 123 |
+
auto g() -> S<T*>::Ptr; // OK, trailing-return-type
|
| 124 |
+
};
|
| 125 |
+
template<typename T> void f() {
|
| 126 |
+
void (*pf)(T::X); // variable pf of type void* initialized with T::X
|
| 127 |
+
void g(T::X); // error: T::X at block scope does not denote a type
|
| 128 |
+
// (attempt to declare a void variable)
|
| 129 |
+
}
|
| 130 |
+
```
|
| 131 |
+
|
| 132 |
+
— *end example*]
|
| 133 |
+
|
| 134 |
+
A *qualified-id* that refers to a member of an unknown specialization,
|
| 135 |
+
that is not prefixed by `typename`, and that is not otherwise assumed to
|
| 136 |
+
name a type (see above) denotes a non-type.
|
| 137 |
+
|
| 138 |
+
[*Example 4*:
|
| 139 |
+
|
| 140 |
``` cpp
|
| 141 |
template <class T> void f(int i) {
|
| 142 |
+
T::x * i; // expression, not the declaration of a variable i
|
| 143 |
}
|
| 144 |
|
| 145 |
struct Foo {
|
| 146 |
typedef int x;
|
| 147 |
};
|
|
|
|
| 158 |
|
| 159 |
— *end example*]
|
| 160 |
|
| 161 |
Within the definition of a class template or within the definition of a
|
| 162 |
member of a class template following the *declarator-id*, the keyword
|
| 163 |
+
`typename` is not required when referring to a member of the current
|
| 164 |
+
instantiation [[temp.dep.type]].
|
|
|
|
| 165 |
|
| 166 |
+
[*Example 5*:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 167 |
|
| 168 |
``` cpp
|
| 169 |
template<class T> struct A {
|
| 170 |
typedef int B;
|
| 171 |
B b; // OK, no typename required
|
| 172 |
};
|
| 173 |
```
|
| 174 |
|
| 175 |
— *end example*]
|
| 176 |
|
| 177 |
+
The validity of a template may be checked prior to any instantiation.
|
| 178 |
+
|
| 179 |
+
[*Note 2*: Knowing which names are type names allows the syntax of
|
| 180 |
+
every template to be checked in this way. — *end note*]
|
| 181 |
+
|
| 182 |
+
The program is ill-formed, no diagnostic required, if:
|
| 183 |
|
| 184 |
- no valid specialization can be generated for a template or a
|
| 185 |
+
substatement of a constexpr if statement [[stmt.if]] within a template
|
| 186 |
+
and the template is not instantiated, or
|
| 187 |
+
- no substitution of template arguments into a *type-constraint* or
|
| 188 |
+
*requires-clause* would result in a valid expression, or
|
| 189 |
- every valid specialization of a variadic template requires an empty
|
| 190 |
template parameter pack, or
|
| 191 |
- a hypothetical instantiation of a template immediately following its
|
| 192 |
definition would be ill-formed due to a construct that does not depend
|
| 193 |
on a template parameter, or
|
|
|
|
| 205 |
*using-declaration* was a pack expansion and the corresponding pack
|
| 206 |
is empty, or
|
| 207 |
- an instantiation uses a default argument or default template
|
| 208 |
argument that had not been defined at the point at which the
|
| 209 |
template was defined, or
|
| 210 |
+
- constant expression evaluation [[expr.const]] within the template
|
| 211 |
instantiation uses
|
| 212 |
+
- the value of a const object of integral or unscoped enumeration
|
| 213 |
type or
|
| 214 |
- the value of a `constexpr` object or
|
| 215 |
- the value of a reference or
|
| 216 |
- the definition of a constexpr function,
|
| 217 |
|
|
|
|
| 227 |
|
| 228 |
Otherwise, no diagnostic shall be issued for a template for which a
|
| 229 |
valid specialization can be generated.
|
| 230 |
|
| 231 |
[*Note 4*: If a template is instantiated, errors will be diagnosed
|
| 232 |
+
according to the other rules in this document. Exactly when these errors
|
| 233 |
+
are diagnosed is a quality of implementation issue. — *end note*]
|
|
|
|
| 234 |
|
| 235 |
+
[*Example 6*:
|
| 236 |
|
| 237 |
``` cpp
|
| 238 |
int j;
|
| 239 |
template<class T> class X {
|
| 240 |
void f(T t, int i, char* p) {
|
|
|
|
| 258 |
|
| 259 |
When looking for the declaration of a name used in a template
|
| 260 |
definition, the usual lookup rules ([[basic.lookup.unqual]],
|
| 261 |
[[basic.lookup.argdep]]) are used for non-dependent names. The lookup of
|
| 262 |
names dependent on the template parameters is postponed until the actual
|
| 263 |
+
template argument is known [[temp.dep]].
|
| 264 |
|
| 265 |
+
[*Example 7*:
|
| 266 |
|
| 267 |
``` cpp
|
| 268 |
#include <iostream>
|
| 269 |
using namespace std;
|
| 270 |
|
|
|
|
| 279 |
cout << p[i] << '\n';
|
| 280 |
}
|
| 281 |
};
|
| 282 |
```
|
| 283 |
|
| 284 |
+
In the example, `i` is the local variable `i` declared in `printall`,
|
| 285 |
`cnt` is the member `cnt` declared in `Set`, and `cout` is the standard
|
| 286 |
output stream declared in `iostream`. However, not every declaration can
|
| 287 |
be found this way; the resolution of some names must be postponed until
|
| 288 |
the actual *template-argument*s are known. For example, even though the
|
| 289 |
name `operator<<` is known within the definition of `printall()` and a
|
| 290 |
declaration of it can be found in `<iostream>`, the actual declaration
|
| 291 |
of `operator<<` needed to print `p[i]` cannot be known until it is known
|
| 292 |
+
what type `T` is [[temp.dep]].
|
| 293 |
|
| 294 |
— *end example*]
|
| 295 |
|
| 296 |
If a name does not depend on a *template-parameter* (as defined in
|
| 297 |
[[temp.dep]]), a declaration (or set of declarations) for that name
|
| 298 |
shall be in scope at the point where the name appears in the template
|
| 299 |
definition; the name is bound to the declaration (or declarations) found
|
| 300 |
at that point and this binding is not affected by declarations that are
|
| 301 |
visible at the point of instantiation.
|
| 302 |
|
| 303 |
+
[*Example 8*:
|
| 304 |
|
| 305 |
``` cpp
|
| 306 |
void f(char);
|
| 307 |
|
| 308 |
template<class T> void g(T t) {
|
|
|
|
| 325 |
— *end example*]
|
| 326 |
|
| 327 |
[*Note 5*: For purposes of name lookup, default arguments and
|
| 328 |
*noexcept-specifier*s of function templates and default arguments and
|
| 329 |
*noexcept-specifier*s of member functions of class templates are
|
| 330 |
+
considered definitions [[temp.decls]]. — *end note*]
|
| 331 |
|
| 332 |
### Locally declared names <a id="temp.local">[[temp.local]]</a>
|
| 333 |
|
| 334 |
Like normal (non-template) classes, class templates have an
|
| 335 |
+
injected-class-name [[class.pre]]. The injected-class-name can be used
|
| 336 |
+
as a *template-name* or a *type-name*. When it is used with a
|
| 337 |
*template-argument-list*, as a *template-argument* for a template
|
| 338 |
*template-parameter*, or as the final identifier in the
|
| 339 |
*elaborated-type-specifier* of a friend class template declaration, it
|
| 340 |
+
is a *template-name* that refers to the class template itself.
|
| 341 |
+
Otherwise, it is a *type-name* equivalent to the *template-name*
|
| 342 |
+
followed by the *template-parameter*s of the class template enclosed in
|
| 343 |
+
`<>`.
|
| 344 |
|
| 345 |
Within the scope of a class template specialization or partial
|
| 346 |
specialization, when the injected-class-name is used as a *type-name*,
|
| 347 |
it is equivalent to the *template-name* followed by the
|
| 348 |
*template-argument*s of the class template specialization or partial
|
|
|
|
| 364 |
```
|
| 365 |
|
| 366 |
— *end example*]
|
| 367 |
|
| 368 |
The injected-class-name of a class template or class template
|
| 369 |
+
specialization can be used as either a *template-name* or a *type-name*
|
| 370 |
wherever it is in scope.
|
| 371 |
|
| 372 |
[*Example 2*:
|
| 373 |
|
| 374 |
``` cpp
|
|
|
|
| 379 |
template <class T> struct Derived: public Base<T> {
|
| 380 |
typename Derived::Base* p; // meaning Derived::Base<T>
|
| 381 |
};
|
| 382 |
|
| 383 |
template<class T, template<class> class U = T::template Base> struct Third { };
|
| 384 |
+
Third<Derived<int> > t; // OK: default argument uses injected-class-name as a template
|
| 385 |
```
|
| 386 |
|
| 387 |
— *end example*]
|
| 388 |
|
| 389 |
+
A lookup that finds an injected-class-name [[class.member.lookup]] can
|
| 390 |
+
result in an ambiguity in certain cases (for example, if it is found in
|
| 391 |
+
more than one base class). If all of the injected-class-names that are
|
| 392 |
+
found refer to specializations of the same class template, and if the
|
| 393 |
+
name is used as a *template-name*, the reference refers to the class
|
| 394 |
template itself and not a specialization thereof, and is not ambiguous.
|
| 395 |
|
| 396 |
[*Example 3*:
|
| 397 |
|
| 398 |
``` cpp
|
|
|
|
| 421 |
};
|
| 422 |
```
|
| 423 |
|
| 424 |
— *end example*]
|
| 425 |
|
| 426 |
+
The name of a *template-parameter* shall not be redeclared within its
|
| 427 |
+
scope (including nested scopes). A *template-parameter* shall not have
|
| 428 |
+
the same name as the template name.
|
| 429 |
|
| 430 |
[*Example 5*:
|
| 431 |
|
| 432 |
``` cpp
|
| 433 |
template<class T, int i> class Y {
|
|
|
|
| 490 |
|
| 491 |
— *end example*]
|
| 492 |
|
| 493 |
In the definition of a class template or in the definition of a member
|
| 494 |
of such a template that appears outside of the template definition, for
|
| 495 |
+
each non-dependent base class [[temp.dep.type]], if the name of the base
|
| 496 |
+
class or the name of a member of the base class is the same as the name
|
| 497 |
+
of a *template-parameter*, the base class name or member name hides the
|
| 498 |
+
*template-parameter* name [[basic.scope.hiding]].
|
| 499 |
|
| 500 |
[*Example 8*:
|
| 501 |
|
| 502 |
``` cpp
|
| 503 |
struct A {
|
|
|
|
| 519 |
Inside a template, some constructs have semantics which may differ from
|
| 520 |
one instantiation to another. Such a construct *depends* on the template
|
| 521 |
parameters. In particular, types and expressions may depend on the type
|
| 522 |
and/or value of template parameters (as determined by the template
|
| 523 |
arguments) and this determines the context for name lookup for certain
|
| 524 |
+
names. An expression may be *type-dependent* (that is, its type may
|
| 525 |
depend on a template parameter) or *value-dependent* (that is, its value
|
| 526 |
+
when evaluated as a constant expression [[expr.const]] may depend on a
|
| 527 |
+
template parameter) as described in this subclause.
|
| 528 |
+
|
| 529 |
+
In an expression of the form:
|
| 530 |
+
|
| 531 |
+
``` bnf
|
| 532 |
+
postfix-expression '(' expression-listₒₚₜ ')'
|
| 533 |
+
```
|
| 534 |
|
| 535 |
where the *postfix-expression* is an *unqualified-id*, the
|
| 536 |
*unqualified-id* denotes a *dependent name* if
|
| 537 |
|
| 538 |
+
- any of the expressions in the *expression-list* is a pack expansion
|
| 539 |
+
[[temp.variadic]],
|
| 540 |
- any of the expressions or *braced-init-list*s in the *expression-list*
|
| 541 |
+
is type-dependent [[temp.dep.expr]], or
|
| 542 |
- the *unqualified-id* is a *template-id* in which any of the template
|
| 543 |
arguments depends on a template parameter.
|
| 544 |
|
| 545 |
If an operand of an operator is a type-dependent expression, the
|
| 546 |
+
operator also denotes a dependent name.
|
| 547 |
+
|
| 548 |
+
[*Note 1*: Such names are unbound and are looked up at the point of the
|
| 549 |
+
template instantiation [[temp.point]] in both the context of the
|
| 550 |
+
template definition and the context of the point of instantiation
|
| 551 |
+
[[temp.dep.candidate]]. — *end note*]
|
| 552 |
|
| 553 |
[*Example 1*:
|
| 554 |
|
| 555 |
``` cpp
|
| 556 |
template<class T> struct X : B<T> {
|
|
|
|
| 560 |
pb->j++;
|
| 561 |
}
|
| 562 |
};
|
| 563 |
```
|
| 564 |
|
| 565 |
+
The base class name `B<T>`, the type name `T::A`, the names `B<T>::i`
|
| 566 |
and `pb->j` explicitly depend on the *template-parameter*.
|
| 567 |
|
| 568 |
— *end example*]
|
| 569 |
|
| 570 |
In the definition of a class or class template, the scope of a dependent
|
| 571 |
+
base class [[temp.dep.type]] is not examined during unqualified name
|
| 572 |
lookup either at the point of definition of the class template or member
|
| 573 |
or during an instantiation of the class template or member.
|
| 574 |
|
| 575 |
[*Example 2*:
|
| 576 |
|
|
|
|
| 620 |
|
| 621 |
A name refers to the *current instantiation* if it is
|
| 622 |
|
| 623 |
- in the definition of a class template, a nested class of a class
|
| 624 |
template, a member of a class template, or a member of a nested class
|
| 625 |
+
of a class template, the injected-class-name [[class.pre]] of the
|
| 626 |
+
class template or nested class,
|
| 627 |
- in the definition of a primary class template or a member of a primary
|
| 628 |
class template, the name of the class template followed by the
|
| 629 |
template argument list of the primary template (as described below)
|
| 630 |
enclosed in `<>` (or an equivalent template alias specialization),
|
| 631 |
- in the definition of a nested class of a class template, the name of
|
| 632 |
the nested class referenced as a member of the current instantiation,
|
| 633 |
or
|
| 634 |
- in the definition of a partial specialization or a member of a partial
|
| 635 |
specialization, the name of the class template followed by the
|
| 636 |
template argument list of the partial specialization enclosed in `<>`
|
| 637 |
+
(or an equivalent template alias specialization). If the nᵗʰ template
|
| 638 |
+
parameter is a template parameter pack, the nᵗʰ template argument is a
|
| 639 |
+
pack expansion [[temp.variadic]] whose pattern is the name of the
|
| 640 |
+
template parameter pack.
|
| 641 |
|
| 642 |
The template argument list of a primary template is a template argument
|
| 643 |
+
list in which the nᵗʰ template argument has the value of the nᵗʰ
|
| 644 |
+
template parameter of the class template. If the nᵗʰ template parameter
|
| 645 |
+
is a template parameter pack [[temp.variadic]], the nᵗʰ template
|
| 646 |
+
argument is a pack expansion [[temp.variadic]] whose pattern is the name
|
| 647 |
+
of the template parameter pack.
|
| 648 |
|
| 649 |
+
A template argument that is equivalent to a template parameter can be
|
| 650 |
+
used in place of that template parameter in a reference to the current
|
| 651 |
+
instantiation. For a template *type-parameter*, a template argument is
|
| 652 |
+
equivalent to a template parameter if it denotes the same type. For a
|
| 653 |
+
non-type template parameter, a template argument is equivalent to a
|
| 654 |
+
template parameter if it is an *identifier* that names a variable that
|
| 655 |
+
is equivalent to the template parameter. A variable is equivalent to a
|
| 656 |
+
template parameter if
|
| 657 |
+
|
| 658 |
+
- it has the same type as the template parameter (ignoring
|
| 659 |
+
cv-qualification) and
|
| 660 |
+
- its initializer consists of a single *identifier* that names the
|
| 661 |
+
template parameter or, recursively, such a variable.
|
| 662 |
+
|
| 663 |
+
[*Note 1*: Using a parenthesized variable name breaks the
|
| 664 |
+
equivalence. — *end note*]
|
| 665 |
|
| 666 |
[*Example 1*:
|
| 667 |
|
| 668 |
``` cpp
|
| 669 |
template <class T> class A {
|
|
|
|
| 688 |
B<T2, T1, I>* b2; // not the current instantiation
|
| 689 |
typedef T1 my_T1;
|
| 690 |
static const int my_I = I;
|
| 691 |
static const int my_I2 = I+0;
|
| 692 |
static const int my_I3 = my_I;
|
| 693 |
+
static const long my_I4 = I;
|
| 694 |
+
static const int my_I5 = (I);
|
| 695 |
B<my_T1, T2, my_I>* b3; // refers to the current instantiation
|
| 696 |
B<my_T1, T2, my_I2>* b4; // not the current instantiation
|
| 697 |
B<my_T1, T2, my_I3>* b5; // refers to the current instantiation
|
| 698 |
+
B<my_T1, T2, my_I4>* b6; // not the current instantiation
|
| 699 |
+
B<my_T1, T2, my_I5>* b7; // not the current instantiation
|
| 700 |
};
|
| 701 |
```
|
| 702 |
|
| 703 |
— *end example*]
|
| 704 |
|
| 705 |
A *dependent base class* is a base class that is a dependent type and is
|
| 706 |
not the current instantiation.
|
| 707 |
|
| 708 |
+
[*Note 2*:
|
| 709 |
|
| 710 |
A base class can be the current instantiation in the case of a nested
|
| 711 |
class naming an enclosing class as a base.
|
| 712 |
|
| 713 |
[*Example 2*:
|
|
|
|
| 732 |
|
| 733 |
A name is a *member of the current instantiation* if it is
|
| 734 |
|
| 735 |
- An unqualified name that, when looked up, refers to at least one
|
| 736 |
member of a class that is the current instantiation or a non-dependent
|
| 737 |
+
base class thereof. \[*Note 3*: This can only occur when looking up a
|
| 738 |
name in a scope enclosed by the definition of a class
|
| 739 |
template. — *end note*]
|
| 740 |
- A *qualified-id* in which the *nested-name-specifier* refers to the
|
| 741 |
current instantiation and that, when looked up, refers to at least one
|
| 742 |
member of a class that is the current instantiation or a non-dependent
|
| 743 |
+
base class thereof. \[*Note 4*: If no such member is found, and the
|
| 744 |
current instantiation has any dependent base classes, then the
|
| 745 |
*qualified-id* is a member of an unknown specialization; see
|
| 746 |
below. — *end note*]
|
| 747 |
- An *id-expression* denoting the member in a class member access
|
| 748 |
+
expression [[expr.ref]] for which the type of the object expression is
|
| 749 |
+
the current instantiation, and the *id-expression*, when looked up
|
| 750 |
+
[[basic.lookup.classref]], refers to at least one member of a class
|
| 751 |
+
that is the current instantiation or a non-dependent base class
|
| 752 |
+
thereof. \[*Note 5*: If no such member is found, and the current
|
| 753 |
instantiation has any dependent base classes, then the *id-expression*
|
| 754 |
is a member of an unknown specialization; see below. — *end note*]
|
| 755 |
|
| 756 |
[*Example 3*:
|
| 757 |
|
|
|
|
| 783 |
current instantiation, the current instantiation has at least one
|
| 784 |
dependent base class, and name lookup of the *qualified-id* does not
|
| 785 |
find any member of a class that is the current instantiation or a
|
| 786 |
non-dependent base class thereof.
|
| 787 |
- An *id-expression* denoting the member in a class member access
|
| 788 |
+
expression [[expr.ref]] in which either
|
| 789 |
- the type of the object expression is the current instantiation, the
|
| 790 |
current instantiation has at least one dependent base class, and
|
| 791 |
name lookup of the *id-expression* does not find a member of a class
|
| 792 |
that is the current instantiation or a non-dependent base class
|
| 793 |
thereof; or
|
| 794 |
+
- the type of the object expression is not the current instantiation
|
| 795 |
+
and the object expression is type-dependent.
|
| 796 |
|
| 797 |
If a *qualified-id* in which the *nested-name-specifier* refers to the
|
| 798 |
current instantiation is not a member of the current instantiation or a
|
| 799 |
member of an unknown specialization, the program is ill-formed even if
|
| 800 |
the template containing the *qualified-id* is not instantiated; no
|
|
|
|
| 861 |
- a cv-qualified type where the cv-unqualified type is dependent,
|
| 862 |
- a compound type constructed from any dependent type,
|
| 863 |
- an array type whose element type is dependent or whose bound (if any)
|
| 864 |
is value-dependent,
|
| 865 |
- a function type whose exception specification is value-dependent,
|
| 866 |
+
- denoted by a *simple-template-id* in which either the template name is
|
| 867 |
+
a template parameter or any of the template arguments is a dependent
|
| 868 |
+
type or an expression that is type-dependent or value-dependent or is
|
| 869 |
+
a pack expansion \[*Note 6*: This includes an injected-class-name
|
| 870 |
+
[[class.pre]] of a class template used without a
|
| 871 |
*template-argument-list*. — *end note*] , or
|
| 872 |
- denoted by `decltype(`*expression*`)`, where *expression* is
|
| 873 |
+
type-dependent [[temp.dep.expr]].
|
| 874 |
|
| 875 |
+
[*Note 7*: Because typedefs do not introduce new types, but instead
|
| 876 |
simply refer to other types, a name that refers to a typedef that is a
|
| 877 |
member of the current instantiation is dependent only if the type
|
| 878 |
referred to is dependent. — *end note*]
|
| 879 |
|
| 880 |
#### Type-dependent expressions <a id="temp.dep.expr">[[temp.dep.expr]]</a>
|
|
|
|
| 883 |
subexpression is type-dependent.
|
| 884 |
|
| 885 |
`this`
|
| 886 |
|
| 887 |
is type-dependent if the class type of the enclosing member function is
|
| 888 |
+
dependent [[temp.dep.type]].
|
| 889 |
|
| 890 |
+
An *id-expression* is type-dependent if it is not a concept-id and it
|
| 891 |
+
contains
|
| 892 |
|
| 893 |
- an *identifier* associated by name lookup with one or more
|
| 894 |
declarations declared with a dependent type,
|
| 895 |
- an *identifier* associated by name lookup with a non-type
|
| 896 |
*template-parameter* declared with a type that contains a placeholder
|
| 897 |
+
type [[dcl.spec.auto]],
|
| 898 |
+
- an *identifier* associated by name lookup with a variable declared
|
| 899 |
+
with a type that contains a placeholder type [[dcl.spec.auto]] where
|
| 900 |
+
the initializer is type-dependent,
|
| 901 |
- an *identifier* associated by name lookup with one or more
|
| 902 |
declarations of member functions of the current instantiation declared
|
| 903 |
with a return type that contains a placeholder type,
|
| 904 |
- an *identifier* associated by name lookup with a structured binding
|
| 905 |
+
declaration [[dcl.struct.bind]] whose *brace-or-equal-initializer* is
|
| 906 |
+
type-dependent,
|
| 907 |
+
- the *identifier* `__func__` [[dcl.fct.def.general]], where any
|
| 908 |
enclosing function is a template, a member of a class template, or a
|
| 909 |
generic lambda,
|
| 910 |
- a *template-id* that is dependent,
|
| 911 |
- a *conversion-function-id* that specifies a dependent type, or
|
| 912 |
- a *nested-name-specifier* or a *qualified-id* that names a member of
|
| 913 |
an unknown specialization;
|
| 914 |
|
| 915 |
or if it names a dependent member of the current instantiation that is a
|
| 916 |
+
static data member of type “array of unknown bound of `T`” for some `T`
|
| 917 |
+
[[temp.static]]. Expressions of the following forms are type-dependent
|
| 918 |
+
only if the type specified by the *type-id*, *simple-type-specifier* or
|
| 919 |
+
*new-type-id* is dependent, even if any subexpression is type-dependent:
|
| 920 |
+
|
| 921 |
+
``` bnf
|
| 922 |
+
simple-type-specifier '(' expression-listₒₚₜ ')'
|
| 923 |
+
'::'ₒₚₜ new new-placementₒₚₜ new-type-id new-initializerₒₚₜ
|
| 924 |
+
'::'ₒₚₜ new new-placementₒₚₜ '(' type-id ')' new-initializerₒₚₜ
|
| 925 |
+
dynamic_cast '<' type-id '>' '(' expression ')'
|
| 926 |
+
static_cast '<' type-id '>' '(' expression ')'
|
| 927 |
+
const_cast '<' type-id '>' '(' expression ')'
|
| 928 |
+
reinterpret_cast '<' type-id '>' '(' expression ')'
|
| 929 |
+
'(' type-id ')' cast-expression
|
| 930 |
+
```
|
| 931 |
|
| 932 |
Expressions of the following forms are never type-dependent (because the
|
| 933 |
type of the expression cannot be dependent):
|
| 934 |
|
| 935 |
+
``` bnf
|
| 936 |
+
literal
|
| 937 |
+
sizeof unary-expression
|
| 938 |
+
sizeof '(' type-id ')'
|
| 939 |
+
sizeof '...' '(' identifier ')'
|
| 940 |
+
alignof '(' type-id ')'
|
| 941 |
+
typeid '(' expression ')'
|
| 942 |
+
typeid '(' type-id ')'
|
| 943 |
+
'::'ₒₚₜ delete cast-expression
|
| 944 |
+
'::'ₒₚₜ delete '[' ']' cast-expression
|
| 945 |
+
throw assignment-expressionₒₚₜ
|
| 946 |
+
noexcept '(' expression ')'
|
| 947 |
+
```
|
| 948 |
+
|
| 949 |
[*Note 1*: For the standard library macro `offsetof`, see
|
| 950 |
[[support.types]]. — *end note*]
|
| 951 |
|
| 952 |
+
A class member access expression [[expr.ref]] is type-dependent if the
|
| 953 |
+
expression refers to a member of the current instantiation and the type
|
| 954 |
+
of the referenced member is dependent, or the class member access
|
| 955 |
expression refers to a member of an unknown specialization.
|
| 956 |
|
| 957 |
[*Note 2*: In an expression of the form `x.y` or `xp->y` the type of
|
| 958 |
the expression is usually the type of the member `y` of the class of `x`
|
| 959 |
(or the class pointed to by `xp`). However, if `x` or `xp` refers to a
|
|
|
|
| 973 |
constant expression is required is value-dependent if any subexpression
|
| 974 |
is value-dependent.
|
| 975 |
|
| 976 |
An *id-expression* is value-dependent if:
|
| 977 |
|
| 978 |
+
- it is a concept-id and any of its arguments are dependent,
|
| 979 |
- it is type-dependent,
|
| 980 |
- it is the name of a non-type template parameter,
|
| 981 |
- it names a static data member that is a dependent member of the
|
| 982 |
current instantiation and is not initialized in a *member-declarator*,
|
| 983 |
- it names a static member function that is a dependent member of the
|
| 984 |
current instantiation, or
|
| 985 |
+
- it names a potentially-constant variable [[expr.const]] that is
|
| 986 |
+
initialized with an expression that is value-dependent.
|
| 987 |
|
| 988 |
Expressions of the following form are value-dependent if the
|
| 989 |
*unary-expression* or *expression* is type-dependent or the *type-id* is
|
| 990 |
dependent:
|
| 991 |
|
| 992 |
+
``` bnf
|
| 993 |
+
sizeof unary-expression
|
| 994 |
+
sizeof '(' type-id ')'
|
| 995 |
+
typeid '(' expression ')'
|
| 996 |
+
typeid '(' type-id ')'
|
| 997 |
+
alignof '(' type-id ')'
|
| 998 |
+
noexcept '(' expression ')'
|
| 999 |
+
```
|
| 1000 |
+
|
| 1001 |
[*Note 1*: For the standard library macro `offsetof`, see
|
| 1002 |
[[support.types]]. — *end note*]
|
| 1003 |
|
| 1004 |
Expressions of the following form are value-dependent if either the
|
| 1005 |
*type-id* or *simple-type-specifier* is dependent or the *expression* or
|
| 1006 |
*cast-expression* is value-dependent:
|
| 1007 |
|
| 1008 |
+
``` bnf
|
| 1009 |
+
simple-type-specifier '(' expression-listₒₚₜ ')'
|
| 1010 |
+
static_cast '<' type-id '>' '(' expression ')'
|
| 1011 |
+
const_cast '<' type-id '>' '(' expression ')'
|
| 1012 |
+
reinterpret_cast '<' type-id '>' '(' expression ')'
|
| 1013 |
+
'(' type-id ')' cast-expression
|
| 1014 |
+
```
|
| 1015 |
+
|
| 1016 |
Expressions of the following form are value-dependent:
|
| 1017 |
|
| 1018 |
+
``` bnf
|
| 1019 |
+
sizeof '...' '(' identifier ')'
|
| 1020 |
+
fold-expression
|
| 1021 |
+
```
|
| 1022 |
+
|
| 1023 |
An expression of the form `&`*qualified-id* where the *qualified-id*
|
| 1024 |
names a dependent member of the current instantiation is
|
| 1025 |
value-dependent. An expression of the form `&`*cast-expression* is also
|
| 1026 |
value-dependent if evaluating *cast-expression* as a core constant
|
| 1027 |
+
expression [[expr.const]] succeeds and the result of the evaluation
|
| 1028 |
refers to a templated entity that is an object with static or thread
|
| 1029 |
storage duration or a member function.
|
| 1030 |
|
| 1031 |
#### Dependent template arguments <a id="temp.dep.temp">[[temp.dep.temp]]</a>
|
| 1032 |
|
|
|
|
| 1070 |
|
| 1071 |
— *end example*]
|
| 1072 |
|
| 1073 |
### Dependent name resolution <a id="temp.dep.res">[[temp.dep.res]]</a>
|
| 1074 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1075 |
#### Point of instantiation <a id="temp.point">[[temp.point]]</a>
|
| 1076 |
|
| 1077 |
For a function template specialization, a member function template
|
| 1078 |
specialization, or a specialization for a member function or static data
|
| 1079 |
member of a class template, if the specialization is implicitly
|
|
|
|
| 1119 |
|
| 1120 |
An explicit instantiation definition is an instantiation point for the
|
| 1121 |
specialization or specializations specified by the explicit
|
| 1122 |
instantiation.
|
| 1123 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1124 |
A specialization for a function template, a member function template, or
|
| 1125 |
of a member function or static data member of a class template may have
|
| 1126 |
multiple points of instantiations within a translation unit, and in
|
| 1127 |
+
addition to the points of instantiation described above,
|
| 1128 |
+
|
| 1129 |
+
- for any such specialization that has a point of instantiation within
|
| 1130 |
+
the *declaration-seq* of the *translation-unit*, prior to the
|
| 1131 |
+
*private-module-fragment* (if any), the point after the
|
| 1132 |
+
*declaration-seq* of the *translation-unit* is also considered a point
|
| 1133 |
+
of instantiation, and
|
| 1134 |
+
- for any such specialization that has a point of instantiation within
|
| 1135 |
+
the *private-module-fragment*, the end of the translation unit is also
|
| 1136 |
+
considered a point of instantiation.
|
| 1137 |
+
|
| 1138 |
+
A specialization for a class template has at most one point of
|
| 1139 |
+
instantiation within a translation unit. A specialization for any
|
| 1140 |
+
template may have points of instantiation in multiple translation units.
|
| 1141 |
+
If two different points of instantiation give a template specialization
|
| 1142 |
+
different meanings according to the one-definition rule
|
| 1143 |
+
[[basic.def.odr]], the program is ill-formed, no diagnostic required.
|
| 1144 |
|
| 1145 |
#### Candidate functions <a id="temp.dep.candidate">[[temp.dep.candidate]]</a>
|
| 1146 |
|
| 1147 |
For a function call where the *postfix-expression* is a dependent name,
|
| 1148 |
+
the candidate functions are found using the usual lookup rules from the
|
| 1149 |
+
template definition context ([[basic.lookup.unqual]],
|
| 1150 |
+
[[basic.lookup.argdep]]).
|
| 1151 |
|
| 1152 |
+
[*Note 1*: For the part of the lookup using associated namespaces
|
| 1153 |
+
[[basic.lookup.argdep]], function declarations found in the template
|
| 1154 |
+
instantiation context are found by this lookup, as described in
|
| 1155 |
+
[[basic.lookup.argdep]]. — *end note*]
|
|
|
|
|
|
|
|
|
|
| 1156 |
|
| 1157 |
If the call would be ill-formed or would find a better match had the
|
| 1158 |
lookup within the associated namespaces considered all the function
|
| 1159 |
declarations with external linkage introduced in those namespaces in all
|
| 1160 |
translation units, not just considering those declarations found in the
|
| 1161 |
template definition and template instantiation contexts, then the
|
| 1162 |
program has undefined behavior.
|
| 1163 |
|
| 1164 |
+
[*Example 1*:
|
| 1165 |
+
|
| 1166 |
+
Source file \`"X.h"\`
|
| 1167 |
+
|
| 1168 |
+
``` cpp
|
| 1169 |
+
namespace Q {
|
| 1170 |
+
struct X { };
|
| 1171 |
+
}
|
| 1172 |
+
```
|
| 1173 |
+
|
| 1174 |
+
Source file \`"G.h"\`
|
| 1175 |
+
|
| 1176 |
+
``` cpp
|
| 1177 |
+
namespace Q {
|
| 1178 |
+
void g_impl(X, X);
|
| 1179 |
+
}
|
| 1180 |
+
```
|
| 1181 |
+
|
| 1182 |
+
Module interface unit of \`M1\`
|
| 1183 |
+
|
| 1184 |
+
``` cpp
|
| 1185 |
+
module;
|
| 1186 |
+
#include "X.h"
|
| 1187 |
+
#include "G.h"
|
| 1188 |
+
export module M1;
|
| 1189 |
+
export template<typename T>
|
| 1190 |
+
void g(T t) {
|
| 1191 |
+
g_impl(t, Q::X{ }); // ADL in definition context finds Q::g_impl, g_impl not discarded
|
| 1192 |
+
}
|
| 1193 |
+
```
|
| 1194 |
+
|
| 1195 |
+
Module interface unit of \`M2\`
|
| 1196 |
+
|
| 1197 |
+
``` cpp
|
| 1198 |
+
module;
|
| 1199 |
+
#include "X.h"
|
| 1200 |
+
export module M2;
|
| 1201 |
+
import M1;
|
| 1202 |
+
void h(Q::X x) {
|
| 1203 |
+
g(x); // OK
|
| 1204 |
+
}
|
| 1205 |
+
```
|
| 1206 |
+
|
| 1207 |
+
— *end example*]
|
| 1208 |
+
|
| 1209 |
+
[*Example 2*:
|
| 1210 |
+
|
| 1211 |
+
Module interface unit of \`Std\`
|
| 1212 |
+
|
| 1213 |
+
``` cpp
|
| 1214 |
+
export module Std;
|
| 1215 |
+
export template<typename Iter>
|
| 1216 |
+
void indirect_swap(Iter lhs, Iter rhs)
|
| 1217 |
+
{
|
| 1218 |
+
swap(*lhs, *rhs); // swap not found by unqualified lookup, can be found only via ADL
|
| 1219 |
+
}
|
| 1220 |
+
```
|
| 1221 |
+
|
| 1222 |
+
Module interface unit of \`M\`
|
| 1223 |
+
|
| 1224 |
+
``` cpp
|
| 1225 |
+
export module M;
|
| 1226 |
+
import Std;
|
| 1227 |
+
|
| 1228 |
+
struct S { /* ...*/ };
|
| 1229 |
+
void swap(S&, S&); // #1
|
| 1230 |
+
|
| 1231 |
+
void f(S* p, S* q)
|
| 1232 |
+
{
|
| 1233 |
+
indirect_swap(p, q); // finds #1 via ADL in instantiation context
|
| 1234 |
+
}
|
| 1235 |
+
```
|
| 1236 |
+
|
| 1237 |
+
— *end example*]
|
| 1238 |
+
|
| 1239 |
+
[*Example 3*:
|
| 1240 |
+
|
| 1241 |
+
Source file \`"X.h"\`
|
| 1242 |
+
|
| 1243 |
+
``` cpp
|
| 1244 |
+
struct X { /* ... */ };
|
| 1245 |
+
X operator+(X, X);
|
| 1246 |
+
```
|
| 1247 |
+
|
| 1248 |
+
Module interface unit of \`F\`
|
| 1249 |
+
|
| 1250 |
+
``` cpp
|
| 1251 |
+
export module F;
|
| 1252 |
+
export template<typename T>
|
| 1253 |
+
void f(T t) {
|
| 1254 |
+
t + t;
|
| 1255 |
+
}
|
| 1256 |
+
```
|
| 1257 |
+
|
| 1258 |
+
Module interface unit of \`M\`
|
| 1259 |
+
|
| 1260 |
+
``` cpp
|
| 1261 |
+
module;
|
| 1262 |
+
#include "X.h"
|
| 1263 |
+
export module M;
|
| 1264 |
+
import F;
|
| 1265 |
+
void g(X x) {
|
| 1266 |
+
f(x); // OK: instantiates f from F,
|
| 1267 |
+
// operator+ is visible in instantiation context
|
| 1268 |
+
}
|
| 1269 |
+
```
|
| 1270 |
+
|
| 1271 |
+
— *end example*]
|
| 1272 |
+
|
| 1273 |
+
[*Example 4*:
|
| 1274 |
+
|
| 1275 |
+
Module interface unit of \`A\`
|
| 1276 |
+
|
| 1277 |
+
``` cpp
|
| 1278 |
+
export module A;
|
| 1279 |
+
export template<typename T>
|
| 1280 |
+
void f(T t) {
|
| 1281 |
+
cat(t, t); // #1
|
| 1282 |
+
dog(t, t); // #2
|
| 1283 |
+
}
|
| 1284 |
+
```
|
| 1285 |
+
|
| 1286 |
+
Module interface unit of \`B\`
|
| 1287 |
+
|
| 1288 |
+
``` cpp
|
| 1289 |
+
export module B;
|
| 1290 |
+
import A;
|
| 1291 |
+
export template<typename T, typename U>
|
| 1292 |
+
void g(T t, U u) {
|
| 1293 |
+
f(t);
|
| 1294 |
+
}
|
| 1295 |
+
```
|
| 1296 |
+
|
| 1297 |
+
Source file \`"foo.h"\`, not an importable header
|
| 1298 |
+
|
| 1299 |
+
``` cpp
|
| 1300 |
+
struct foo {
|
| 1301 |
+
friend int cat(foo, foo);
|
| 1302 |
+
};
|
| 1303 |
+
int dog(foo, foo);
|
| 1304 |
+
```
|
| 1305 |
+
|
| 1306 |
+
Module interface unit of \`C1\`
|
| 1307 |
+
|
| 1308 |
+
``` cpp
|
| 1309 |
+
module;
|
| 1310 |
+
#include "foo.h" // dog not referenced, discarded
|
| 1311 |
+
export module C1;
|
| 1312 |
+
import B;
|
| 1313 |
+
export template<typename T>
|
| 1314 |
+
void h(T t) {
|
| 1315 |
+
g(foo{ }, t);
|
| 1316 |
+
}
|
| 1317 |
+
```
|
| 1318 |
+
|
| 1319 |
+
Translation unit
|
| 1320 |
+
|
| 1321 |
+
``` cpp
|
| 1322 |
+
import C1;
|
| 1323 |
+
void i() {
|
| 1324 |
+
h(0); // error: dog not found at #2
|
| 1325 |
+
}
|
| 1326 |
+
```
|
| 1327 |
+
|
| 1328 |
+
Importable header \`"bar.h"\`
|
| 1329 |
+
|
| 1330 |
+
``` cpp
|
| 1331 |
+
struct bar {
|
| 1332 |
+
friend int cat(bar, bar);
|
| 1333 |
+
};
|
| 1334 |
+
int dog(bar, bar);
|
| 1335 |
+
```
|
| 1336 |
+
|
| 1337 |
+
Module interface unit of \`C2\`
|
| 1338 |
+
|
| 1339 |
+
``` cpp
|
| 1340 |
+
module;
|
| 1341 |
+
#include "bar.h" // imports header unit "bar.h"
|
| 1342 |
+
export module C2;
|
| 1343 |
+
import B;
|
| 1344 |
+
export template<typename T>
|
| 1345 |
+
void j(T t) {
|
| 1346 |
+
g(bar{ }, t);
|
| 1347 |
+
}
|
| 1348 |
+
```
|
| 1349 |
+
|
| 1350 |
+
Translation unit
|
| 1351 |
+
|
| 1352 |
+
``` cpp
|
| 1353 |
+
import C2;
|
| 1354 |
+
void k() {
|
| 1355 |
+
j(0); // OK, dog found in instantiation context:
|
| 1356 |
+
// visible at end of module interface unit of C2
|
| 1357 |
+
}
|
| 1358 |
+
```
|
| 1359 |
+
|
| 1360 |
+
— *end example*]
|
| 1361 |
+
|
| 1362 |
### Friend names declared within a class template <a id="temp.inject">[[temp.inject]]</a>
|
| 1363 |
|
| 1364 |
Friend classes or functions can be declared within a class template.
|
| 1365 |
When a template is instantiated, the names of its friends are treated as
|
| 1366 |
if the specialization had been explicitly declared at its point of
|
| 1367 |
instantiation.
|
| 1368 |
|
| 1369 |
As with non-template classes, the names of namespace-scope friend
|
| 1370 |
functions of a class template specialization are not visible during an
|
| 1371 |
+
ordinary lookup unless explicitly declared at namespace scope
|
| 1372 |
+
[[class.friend]]. Such names may be found under the rules for associated
|
| 1373 |
+
classes [[basic.lookup.argdep]].[^11]
|
| 1374 |
|
| 1375 |
[*Example 1*:
|
| 1376 |
|
| 1377 |
``` cpp
|
| 1378 |
template<typename T> struct number {
|
|
|
|
| 1382 |
|
| 1383 |
void g() {
|
| 1384 |
number<double> a(3), b(4);
|
| 1385 |
a = gcd(a,b); // finds gcd because number<double> is an associated class,
|
| 1386 |
// making gcd visible in its namespace (global scope)
|
| 1387 |
+
b = gcd(3,4); // error: gcd is not visible
|
| 1388 |
}
|
| 1389 |
```
|
| 1390 |
|
| 1391 |
— *end example*]
|
| 1392 |
|