- tmp/tmpa9t2iebf/{from.md → to.md} +331 -135
tmp/tmpa9t2iebf/{from.md → to.md}
RENAMED
|
@@ -10,10 +10,12 @@ Three kinds of names can be used within a template definition:
|
|
| 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
|
| 13 |
the keyword `typename`.
|
| 14 |
|
|
|
|
|
|
|
| 15 |
``` cpp
|
| 16 |
// no B declared here
|
| 17 |
|
| 18 |
class X;
|
| 19 |
|
|
@@ -27,36 +29,39 @@ template<class T> class Y {
|
|
| 27 |
Z* a4; // declare pointer to Z
|
| 28 |
typedef typename T::A TA;
|
| 29 |
TA* a5; // declare pointer to T's A
|
| 30 |
typename T::A* a6; // declare pointer to T's A
|
| 31 |
T::A* a7; // T::A is not a type name:
|
| 32 |
-
//
|
| 33 |
-
// no visible declaration of a7
|
| 34 |
B* a8; // B is not a type name:
|
| 35 |
-
//
|
| 36 |
-
// no visible declarations of B and a8
|
| 37 |
}
|
| 38 |
};
|
| 39 |
```
|
| 40 |
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
``` bnf
|
| 48 |
typename-specifier:
|
| 49 |
'typename' nested-name-specifier identifier
|
| 50 |
'typename' nested-name-specifier 'template'ₒₚₜ simple-template-id
|
| 51 |
```
|
| 52 |
|
| 53 |
If a specialization of a template is instantiated for a set of
|
| 54 |
*template-argument*s such that the *qualified-id* prefixed by `typename`
|
| 55 |
-
does not denote a type, the specialization is
|
| 56 |
-
qualified name lookup ([[basic.lookup.qual]]) is
|
| 57 |
-
*qualified-id* even in the presence of `typename`.
|
|
|
|
|
|
|
| 58 |
|
| 59 |
``` cpp
|
| 60 |
struct A {
|
| 61 |
struct X { };
|
| 62 |
int X;
|
|
@@ -73,25 +78,31 @@ void foo() {
|
|
| 73 |
f(b); // OK: T::X refers to B::X
|
| 74 |
f(a); // error: T::X refers to the data member A::X not the struct A::X
|
| 75 |
}
|
| 76 |
```
|
| 77 |
|
| 78 |
-
|
| 79 |
-
|
|
|
|
|
|
|
| 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 |
|
| 87 |
If, for a given set of template arguments, a specialization of a
|
| 88 |
template is instantiated that refers to a *qualified-id* that denotes a
|
| 89 |
-
type, and the *qualified-id* refers to a member of
|
| 90 |
-
specialization, the *qualified-id* shall either be prefixed
|
| 91 |
-
`typename` or shall be used in a context in which it implicitly names
|
| 92 |
-
type as described above.
|
|
|
|
|
|
|
| 93 |
|
| 94 |
``` cpp
|
| 95 |
template <class T> void f(int i) {
|
| 96 |
T::x * i; // T::x must not be a type
|
| 97 |
}
|
|
@@ -108,73 +119,121 @@ int main() {
|
|
| 108 |
f<Bar>(1); // OK
|
| 109 |
f<Foo>(1); // error: Foo::x is a type
|
| 110 |
}
|
| 111 |
```
|
| 112 |
|
|
|
|
|
|
|
| 113 |
Within the definition of a class template or within the definition of a
|
| 114 |
member of a class template following the *declarator-id*, the keyword
|
| 115 |
`typename` is not required when referring to the name of a previously
|
| 116 |
-
declared member of the class template that declares a type
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
[
|
| 120 |
-
[[basic.lookup.
|
| 121 |
-
current instantiation ([[temp.dep.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
|
| 123 |
``` cpp
|
| 124 |
template<class T> struct A {
|
| 125 |
typedef int B;
|
| 126 |
B b; // OK, no typename required
|
| 127 |
};
|
| 128 |
```
|
| 129 |
|
|
|
|
|
|
|
| 130 |
Knowing which names are type names allows the syntax of every template
|
| 131 |
-
to be checked.
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 145 |
|
| 146 |
``` cpp
|
| 147 |
int j;
|
| 148 |
template<class T> class X {
|
| 149 |
void f(T t, int i, char* p) {
|
| 150 |
-
t = i; // diagnosed if X::f is instantiated
|
| 151 |
-
|
| 152 |
-
p =
|
| 153 |
-
// not instantiated
|
| 154 |
-
p = j; // may be diagnosed even if X::f is
|
| 155 |
-
// not instantiated
|
| 156 |
}
|
| 157 |
void g(T t) {
|
| 158 |
-
+; // may be diagnosed even if X::g is
|
| 159 |
-
// not instantiated
|
| 160 |
}
|
| 161 |
};
|
| 162 |
|
| 163 |
template<class... T> struct A {
|
| 164 |
void operator++(int, T... t); // error: too many parameters
|
| 165 |
};
|
| 166 |
template<class... T> union X : T... { }; // error: union with base class
|
| 167 |
template<class... T> struct A : T..., T... { }; // error: duplicate base class
|
| 168 |
```
|
| 169 |
|
|
|
|
|
|
|
| 170 |
When looking for the declaration of a name used in a template
|
| 171 |
definition, the usual lookup rules ([[basic.lookup.unqual]],
|
| 172 |
[[basic.lookup.argdep]]) are used for non-dependent names. The lookup of
|
| 173 |
names dependent on the template parameters is postponed until the actual
|
| 174 |
template argument is known ([[temp.dep]]).
|
| 175 |
|
|
|
|
|
|
|
| 176 |
``` cpp
|
| 177 |
#include <iostream>
|
| 178 |
using namespace std;
|
| 179 |
|
| 180 |
template<class T> class Set {
|
|
@@ -198,43 +257,47 @@ the actual *template-argument*s are known. For example, even though the
|
|
| 198 |
name `operator<<` is known within the definition of `printall()` and a
|
| 199 |
declaration of it can be found in `<iostream>`, the actual declaration
|
| 200 |
of `operator<<` needed to print `p[i]` cannot be known until it is known
|
| 201 |
what type `T` is ([[temp.dep]]).
|
| 202 |
|
|
|
|
|
|
|
| 203 |
If a name does not depend on a *template-parameter* (as defined in
|
| 204 |
[[temp.dep]]), a declaration (or set of declarations) for that name
|
| 205 |
shall be in scope at the point where the name appears in the template
|
| 206 |
definition; the name is bound to the declaration (or declarations) found
|
| 207 |
at that point and this binding is not affected by declarations that are
|
| 208 |
visible at the point of instantiation.
|
| 209 |
|
|
|
|
|
|
|
| 210 |
``` cpp
|
| 211 |
void f(char);
|
| 212 |
|
| 213 |
template<class T> void g(T t) {
|
| 214 |
f(1); // f(char)
|
| 215 |
f(T(1)); // dependent
|
| 216 |
f(t); // dependent
|
| 217 |
-
dd++; // not dependent
|
| 218 |
-
// error: declaration for dd not found
|
| 219 |
}
|
| 220 |
|
| 221 |
enum E { e };
|
| 222 |
void f(E);
|
| 223 |
|
| 224 |
double dd;
|
| 225 |
void h() {
|
| 226 |
-
g(e); // will cause one call of f(char) followed
|
| 227 |
-
// by two calls of f(E)
|
| 228 |
g('a'); // will cause three calls of f(char)
|
| 229 |
}
|
| 230 |
```
|
| 231 |
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
|
|
|
|
|
|
| 236 |
|
| 237 |
### Locally declared names <a id="temp.local">[[temp.local]]</a>
|
| 238 |
|
| 239 |
Like normal (non-template) classes, class templates have an
|
| 240 |
injected-class-name (Clause [[class]]). The injected-class-name can be
|
|
@@ -250,10 +313,12 @@ Within the scope of a class template specialization or partial
|
|
| 250 |
specialization, when the injected-class-name is used as a *type-name*,
|
| 251 |
it is equivalent to the *template-name* followed by the
|
| 252 |
*template-argument*s of the class template specialization or partial
|
| 253 |
specialization enclosed in `<>`.
|
| 254 |
|
|
|
|
|
|
|
| 255 |
``` cpp
|
| 256 |
template<template<class> class T> class A { };
|
| 257 |
template<class T> class Y;
|
| 258 |
template<> class Y<int> {
|
| 259 |
Y* p; // meaning Y<int>
|
|
@@ -263,14 +328,18 @@ template<> class Y<int> {
|
|
| 263 |
template<class> friend class Y; // meaning ::Y
|
| 264 |
};
|
| 265 |
};
|
| 266 |
```
|
| 267 |
|
|
|
|
|
|
|
| 268 |
The injected-class-name of a class template or class template
|
| 269 |
specialization can be used either as a *template-name* or a *type-name*
|
| 270 |
wherever it is in scope.
|
| 271 |
|
|
|
|
|
|
|
| 272 |
``` cpp
|
| 273 |
template <class T> struct Base {
|
| 274 |
Base* p;
|
| 275 |
};
|
| 276 |
|
|
@@ -280,43 +349,55 @@ template <class T> struct Derived: public Base<T> {
|
|
| 280 |
|
| 281 |
template<class T, template<class> class U = T::template Base> struct Third { };
|
| 282 |
Third<Base<int> > t; // OK: default argument uses injected-class-name as a template
|
| 283 |
```
|
| 284 |
|
|
|
|
|
|
|
| 285 |
A lookup that finds an injected-class-name ([[class.member.lookup]])
|
| 286 |
can result in an ambiguity in certain cases (for example, if it is found
|
| 287 |
in more than one base class). If all of the injected-class-names that
|
| 288 |
are found refer to specializations of the same class template, and if
|
| 289 |
the name is used as a *template-name*, the reference refers to the class
|
| 290 |
template itself and not a specialization thereof, and is not ambiguous.
|
| 291 |
|
|
|
|
|
|
|
| 292 |
``` cpp
|
| 293 |
template <class T> struct Base { };
|
| 294 |
template <class T> struct Derived: Base<int>, Base<char> {
|
| 295 |
typename Derived::Base b; // error: ambiguous
|
| 296 |
typename Derived::Base<double> d; // OK
|
| 297 |
};
|
| 298 |
```
|
| 299 |
|
|
|
|
|
|
|
| 300 |
When the normal name of the template (i.e., the name from the enclosing
|
| 301 |
scope, not the injected-class-name) is used, it always refers to the
|
| 302 |
class template itself and not a specialization of the template.
|
| 303 |
|
|
|
|
|
|
|
| 304 |
``` cpp
|
| 305 |
template<class T> class X {
|
| 306 |
X* p; // meaning X<T>
|
| 307 |
X<T>* p2;
|
| 308 |
X<int>* p3;
|
| 309 |
::X* p4; // error: missing template argument list
|
| 310 |
// ::X does not refer to the injected-class-name
|
| 311 |
};
|
| 312 |
```
|
| 313 |
|
|
|
|
|
|
|
| 314 |
A *template-parameter* shall not be redeclared within its scope
|
| 315 |
(including nested scopes). A *template-parameter* shall not have the
|
| 316 |
same name as the template name.
|
| 317 |
|
|
|
|
|
|
|
| 318 |
``` cpp
|
| 319 |
template<class T, int i> class Y {
|
| 320 |
int T; // error: template-parameter redeclared
|
| 321 |
void f() {
|
| 322 |
char T; // error: template-parameter redeclared
|
|
@@ -324,19 +405,23 @@ template<class T, int i> class Y {
|
|
| 324 |
};
|
| 325 |
|
| 326 |
template<class X> class X; // error: template-parameter redeclared
|
| 327 |
```
|
| 328 |
|
|
|
|
|
|
|
| 329 |
In the definition of a member of a class template that appears outside
|
| 330 |
of the class template definition, the name of a member of the class
|
| 331 |
template hides the name of a *template-parameter* of any enclosing class
|
| 332 |
templates (but not a *template-parameter* of the member if the member is
|
| 333 |
a class or function template).
|
| 334 |
|
|
|
|
|
|
|
| 335 |
``` cpp
|
| 336 |
template<class T> struct A {
|
| 337 |
-
struct B {
|
| 338 |
typedef void C;
|
| 339 |
void f();
|
| 340 |
template<class U> void g(U);
|
| 341 |
};
|
| 342 |
|
|
@@ -348,14 +433,18 @@ template<class B> template<class C> void A<B>::g(C) {
|
|
| 348 |
B b; // A's B, not the template parameter
|
| 349 |
C c; // the template parameter C, not A's C
|
| 350 |
}
|
| 351 |
```
|
| 352 |
|
|
|
|
|
|
|
| 353 |
In the definition of a member of a class template that appears outside
|
| 354 |
of the namespace containing the class template definition, the name of a
|
| 355 |
*template-parameter* hides the name of a member of this namespace.
|
| 356 |
|
|
|
|
|
|
|
| 357 |
``` cpp
|
| 358 |
namespace N {
|
| 359 |
class C { };
|
| 360 |
template<class T> class B {
|
| 361 |
void f(T);
|
|
@@ -364,58 +453,67 @@ namespace N {
|
|
| 364 |
template<class C> void N::B<C>::f(C) {
|
| 365 |
C b; // C is the template parameter, not N::C
|
| 366 |
}
|
| 367 |
```
|
| 368 |
|
|
|
|
|
|
|
| 369 |
In the definition of a class template or in the definition of a member
|
| 370 |
of such a template that appears outside of the template definition, for
|
| 371 |
-
each base class
|
| 372 |
-
|
| 373 |
-
|
| 374 |
-
|
| 375 |
-
|
|
|
|
| 376 |
|
| 377 |
``` cpp
|
| 378 |
struct A {
|
| 379 |
-
struct B {
|
| 380 |
int a;
|
| 381 |
int Y;
|
| 382 |
};
|
| 383 |
|
| 384 |
template<class B, class a> struct X : A {
|
| 385 |
B b; // A's B
|
| 386 |
a b; // error: A's a isn't a type name
|
| 387 |
};
|
| 388 |
```
|
| 389 |
|
|
|
|
|
|
|
| 390 |
### Dependent names <a id="temp.dep">[[temp.dep]]</a>
|
| 391 |
|
| 392 |
Inside a template, some constructs have semantics which may differ from
|
| 393 |
one instantiation to another. Such a construct *depends* on the template
|
| 394 |
parameters. In particular, types and expressions may depend on the type
|
| 395 |
and/or value of template parameters (as determined by the template
|
| 396 |
arguments) and this determines the context for name lookup for certain
|
| 397 |
-
names.
|
| 398 |
-
parameter) or *value-dependent* (
|
| 399 |
-
|
|
|
|
|
|
|
| 400 |
|
| 401 |
where the *postfix-expression* is an *unqualified-id*, the
|
| 402 |
*unqualified-id* denotes a *dependent name* if
|
| 403 |
|
| 404 |
- any of the expressions in the *expression-list* is a pack expansion (
|
| 405 |
[[temp.variadic]]),
|
| 406 |
-
- any of the expressions
|
| 407 |
-
|
| 408 |
-
-
|
| 409 |
-
|
| 410 |
|
| 411 |
If an operand of an operator is a type-dependent expression, the
|
| 412 |
operator also denotes a dependent name. Such names are unbound and are
|
| 413 |
looked up at the point of the template instantiation ([[temp.point]])
|
| 414 |
in both the context of the template definition and the context of the
|
| 415 |
point of instantiation.
|
| 416 |
|
|
|
|
|
|
|
| 417 |
``` cpp
|
| 418 |
template<class T> struct X : B<T> {
|
| 419 |
typename T::A* pa;
|
| 420 |
void f(B<T>* pb) {
|
| 421 |
static int i = B<T>::i;
|
|
@@ -425,15 +523,18 @@ template<class T> struct X : B<T> {
|
|
| 425 |
```
|
| 426 |
|
| 427 |
the base class name `B<T>`, the type name `T::A`, the names `B<T>::i`
|
| 428 |
and `pb->j` explicitly depend on the *template-parameter*.
|
| 429 |
|
| 430 |
-
|
| 431 |
-
|
| 432 |
-
|
| 433 |
-
|
| 434 |
-
member
|
|
|
|
|
|
|
|
|
|
| 435 |
|
| 436 |
``` cpp
|
| 437 |
typedef double A;
|
| 438 |
template<class T> class B {
|
| 439 |
typedef int A;
|
|
@@ -445,21 +546,25 @@ template<class T> struct X : B<T> {
|
|
| 445 |
|
| 446 |
The type name `A` in the definition of `X<T>` binds to the typedef name
|
| 447 |
defined in the global namespace scope, not to the typedef name defined
|
| 448 |
in the base class `B<T>`.
|
| 449 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 450 |
``` cpp
|
| 451 |
struct A {
|
| 452 |
-
struct B {
|
| 453 |
int a;
|
| 454 |
int Y;
|
| 455 |
};
|
| 456 |
|
| 457 |
int a;
|
| 458 |
|
| 459 |
template<class T> struct Y : T {
|
| 460 |
-
struct B {
|
| 461 |
B b; // The B defined in Y
|
| 462 |
void f(int i) { a = i; } // ::a
|
| 463 |
Y* p; // Y<T>
|
| 464 |
};
|
| 465 |
|
|
@@ -467,10 +572,12 @@ Y<A> ya;
|
|
| 467 |
```
|
| 468 |
|
| 469 |
The members `A::B`, `A::a`, and `A::Y` of the template argument `A` do
|
| 470 |
not affect the binding of names in `Y<A>`.
|
| 471 |
|
|
|
|
|
|
|
| 472 |
#### Dependent types <a id="temp.dep.type">[[temp.dep.type]]</a>
|
| 473 |
|
| 474 |
A name refers to the *current instantiation* if it is
|
| 475 |
|
| 476 |
- in the definition of a class template, a nested class of a class
|
|
@@ -505,21 +612,22 @@ can be used in place of that template parameter in a reference to the
|
|
| 505 |
current instantiation. In the case of a non-type template argument, the
|
| 506 |
argument must have been given the value of the template parameter and
|
| 507 |
not an expression in which the template parameter appears as a
|
| 508 |
subexpression.
|
| 509 |
|
|
|
|
|
|
|
| 510 |
``` cpp
|
| 511 |
template <class T> class A {
|
| 512 |
A* p1; // A is the current instantiation
|
| 513 |
A<T>* p2; // A<T> is the current instantiation
|
| 514 |
A<T*> p3; // A<T*> is not the current instantiation
|
| 515 |
::A<T>* p4; // ::A<T> is the current instantiation
|
| 516 |
class B {
|
| 517 |
B* p1; // B is the current instantiation
|
| 518 |
A<T>::B* p2; // A<T>::B is the current instantiation
|
| 519 |
-
typename A<T*>::B* p3; // A<T*>::B is not the
|
| 520 |
-
// current instantiation
|
| 521 |
};
|
| 522 |
};
|
| 523 |
|
| 524 |
template <class T> class A<T*> {
|
| 525 |
A<T*>* p1; // A<T*> is the current instantiation
|
|
@@ -537,30 +645,64 @@ template <class T1, class T2, int I> struct B {
|
|
| 537 |
B<my_T1, T2, my_I2>* b4; // not the current instantiation
|
| 538 |
B<my_T1, T2, my_I3>* b5; // refers to the current instantiation
|
| 539 |
};
|
| 540 |
```
|
| 541 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 542 |
A name is a *member of the current instantiation* if it is
|
| 543 |
|
| 544 |
- An unqualified name that, when looked up, refers to at least one
|
| 545 |
member of a class that is the current instantiation or a non-dependent
|
| 546 |
-
base class thereof. This can only occur when looking up a
|
| 547 |
-
scope enclosed by the definition of a class
|
|
|
|
| 548 |
- A *qualified-id* in which the *nested-name-specifier* refers to the
|
| 549 |
current instantiation and that, when looked up, refers to at least one
|
| 550 |
member of a class that is the current instantiation or a non-dependent
|
| 551 |
-
base class thereof.
|
| 552 |
-
instantiation has any dependent base classes, then the
|
| 553 |
-
is a member of an unknown specialization; see
|
|
|
|
| 554 |
- An *id-expression* denoting the member in a class member access
|
| 555 |
expression ([[expr.ref]]) for which the type of the object expression
|
| 556 |
is the current instantiation, and the *id-expression*, when looked
|
| 557 |
up ([[basic.lookup.classref]]), refers to at least one member of a
|
| 558 |
class that is the current instantiation or a non-dependent base class
|
| 559 |
-
thereof.
|
| 560 |
-
any dependent base classes, then the *id-expression*
|
| 561 |
-
unknown specialization; see below.
|
|
|
|
|
|
|
| 562 |
|
| 563 |
``` cpp
|
| 564 |
template <class T> class A {
|
| 565 |
static const int i = 5;
|
| 566 |
int n1[i]; // i refers to a member of the current instantiation
|
|
@@ -572,10 +714,12 @@ template <class T> class A {
|
|
| 572 |
template <class T> int A<T>::f() {
|
| 573 |
return i; // i refers to a member of the current instantiation
|
| 574 |
}
|
| 575 |
```
|
| 576 |
|
|
|
|
|
|
|
| 577 |
A name is a *dependent member of the current instantiation* if it is a
|
| 578 |
member of the current instantiation that, when looked up, refers to at
|
| 579 |
least one member of a class that is the current instantiation.
|
| 580 |
|
| 581 |
A name is a *member of an unknown specialization* if it is
|
|
@@ -606,10 +750,12 @@ access expression for which the type of the object expression is the
|
|
| 606 |
current instantiation does not refer to a member of the current
|
| 607 |
instantiation or a member of an unknown specialization, the program is
|
| 608 |
ill-formed even if the template containing the member access expression
|
| 609 |
is not instantiated; no diagnostic required.
|
| 610 |
|
|
|
|
|
|
|
| 611 |
``` cpp
|
| 612 |
template<class T> class A {
|
| 613 |
typedef int type;
|
| 614 |
void f() {
|
| 615 |
A<T>::type i; // OK: refers to a member of the current instantiation
|
|
@@ -617,42 +763,68 @@ template<class T> class A {
|
|
| 617 |
// a member of an unknown specialization
|
| 618 |
}
|
| 619 |
};
|
| 620 |
```
|
| 621 |
|
|
|
|
|
|
|
| 622 |
If, for a given set of template arguments, a specialization of a
|
| 623 |
template is instantiated that refers to a member of the current
|
| 624 |
instantiation with a *qualified-id* or class member access expression,
|
| 625 |
the name in the *qualified-id* or class member access expression is
|
| 626 |
looked up in the template instantiation context. If the result of this
|
| 627 |
lookup differs from the result of name lookup in the template definition
|
| 628 |
-
context, name lookup is ambiguous.
|
| 629 |
-
|
| 630 |
-
|
| 631 |
-
|
| 632 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 633 |
|
| 634 |
A type is dependent if it is
|
| 635 |
|
| 636 |
- a template parameter,
|
| 637 |
- a member of an unknown specialization,
|
| 638 |
- a nested class or enumeration that is a dependent member of the
|
| 639 |
current instantiation,
|
| 640 |
- a cv-qualified type where the cv-unqualified type is dependent,
|
| 641 |
- a compound type constructed from any dependent type,
|
| 642 |
-
- an array type
|
| 643 |
-
|
|
|
|
| 644 |
- a *simple-template-id* in which either the template name is a template
|
| 645 |
parameter or any of the template arguments is a dependent type or an
|
| 646 |
-
expression that is type-dependent or value-dependent
|
|
|
|
|
|
|
|
|
|
| 647 |
- denoted by `decltype(`*expression*`)`, where *expression* is
|
| 648 |
type-dependent ([[temp.dep.expr]]).
|
| 649 |
|
| 650 |
-
Because typedefs do not introduce new types, but instead
|
| 651 |
-
other types, a name that refers to a typedef that is a
|
| 652 |
-
current instantiation is dependent only if the type
|
| 653 |
-
dependent.
|
| 654 |
|
| 655 |
#### Type-dependent expressions <a id="temp.dep.expr">[[temp.dep.expr]]</a>
|
| 656 |
|
| 657 |
Except as described below, an expression is type-dependent if any
|
| 658 |
subexpression is type-dependent.
|
|
@@ -664,14 +836,22 @@ dependent ([[temp.dep.type]]).
|
|
| 664 |
|
| 665 |
An *id-expression* is type-dependent if it contains
|
| 666 |
|
| 667 |
- an *identifier* associated by name lookup with one or more
|
| 668 |
declarations declared with a dependent type,
|
|
|
|
|
|
|
|
|
|
| 669 |
- an *identifier* associated by name lookup with one or more
|
| 670 |
declarations of member functions of the current instantiation declared
|
| 671 |
-
with a return type that contains a placeholder type
|
| 672 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 673 |
- a *template-id* that is dependent,
|
| 674 |
- a *conversion-function-id* that specifies a dependent type, or
|
| 675 |
- a *nested-name-specifier* or a *qualified-id* that names a member of
|
| 676 |
an unknown specialization;
|
| 677 |
|
|
@@ -683,34 +863,41 @@ type-dependent only if the type specified by the *type-id*,
|
|
| 683 |
subexpression is type-dependent:
|
| 684 |
|
| 685 |
Expressions of the following forms are never type-dependent (because the
|
| 686 |
type of the expression cannot be dependent):
|
| 687 |
|
| 688 |
-
For the standard library macro `offsetof`, see
|
|
|
|
| 689 |
|
| 690 |
A class member access expression ([[expr.ref]]) is type-dependent if
|
| 691 |
the expression refers to a member of the current instantiation and the
|
| 692 |
type of the referenced member is dependent, or the class member access
|
| 693 |
-
expression refers to a member of an unknown specialization.
|
| 694 |
-
|
| 695 |
-
|
| 696 |
-
|
| 697 |
-
|
| 698 |
-
dependent
|
| 699 |
-
|
| 700 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 701 |
|
| 702 |
#### Value-dependent expressions <a id="temp.dep.constexpr">[[temp.dep.constexpr]]</a>
|
| 703 |
|
| 704 |
-
Except as described below,
|
| 705 |
-
|
|
|
|
| 706 |
|
| 707 |
An *id-expression* is value-dependent if:
|
| 708 |
|
| 709 |
-
- it is
|
| 710 |
- it is the name of a non-type template parameter,
|
| 711 |
-
- it names a member of an unknown specialization,
|
| 712 |
- it names a static data member that is a dependent member of the
|
| 713 |
current instantiation and is not initialized in a *member-declarator*,
|
| 714 |
- it names a static member function that is a dependent member of the
|
| 715 |
current instantiation, or
|
| 716 |
- it is a constant with literal type and is initialized with an
|
|
@@ -718,21 +905,26 @@ An *id-expression* is value-dependent if:
|
|
| 718 |
|
| 719 |
Expressions of the following form are value-dependent if the
|
| 720 |
*unary-expression* or *expression* is type-dependent or the *type-id* is
|
| 721 |
dependent:
|
| 722 |
|
| 723 |
-
For the standard library macro `offsetof`, see
|
|
|
|
| 724 |
|
| 725 |
Expressions of the following form are value-dependent if either the
|
| 726 |
*type-id* or *simple-type-specifier* is dependent or the *expression* or
|
| 727 |
*cast-expression* is value-dependent:
|
| 728 |
|
| 729 |
Expressions of the following form are value-dependent:
|
| 730 |
|
| 731 |
An expression of the form `&`*qualified-id* where the *qualified-id*
|
| 732 |
names a dependent member of the current instantiation is
|
| 733 |
-
value-dependent.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 734 |
|
| 735 |
#### Dependent template arguments <a id="temp.dep.temp">[[temp.dep.temp]]</a>
|
| 736 |
|
| 737 |
A type *template-argument* is dependent if the type it specifies is
|
| 738 |
dependent.
|
|
@@ -752,28 +944,30 @@ an unknown specialization.
|
|
| 752 |
### Non-dependent names <a id="temp.nondep">[[temp.nondep]]</a>
|
| 753 |
|
| 754 |
Non-dependent names used in a template definition are found using the
|
| 755 |
usual name lookup and bound at the point they are used.
|
| 756 |
|
|
|
|
|
|
|
| 757 |
``` cpp
|
| 758 |
void g(double);
|
| 759 |
void h();
|
| 760 |
|
| 761 |
template<class T> class Z {
|
| 762 |
public:
|
| 763 |
void f() {
|
| 764 |
g(1); // calls g(double)
|
| 765 |
-
h++; // ill-formed: cannot increment function;
|
| 766 |
-
//
|
| 767 |
-
// at the point of instantiation
|
| 768 |
}
|
| 769 |
};
|
| 770 |
|
| 771 |
-
void g(int); // not in scope at the point of the template
|
| 772 |
-
// definition, not considered for the call g(1)
|
| 773 |
```
|
| 774 |
|
|
|
|
|
|
|
| 775 |
### Dependent name resolution <a id="temp.dep.res">[[temp.dep.res]]</a>
|
| 776 |
|
| 777 |
In resolving dependent names, names from the following sources are
|
| 778 |
considered:
|
| 779 |
|
|
@@ -800,20 +994,19 @@ If a function template or member function of a class template is called
|
|
| 800 |
in a way which uses the definition of a default argument of that
|
| 801 |
function template or member function, the point of instantiation of the
|
| 802 |
default argument is the point of instantiation of the function template
|
| 803 |
or member function specialization.
|
| 804 |
|
| 805 |
-
For
|
| 806 |
-
|
| 807 |
-
*
|
| 808 |
-
|
| 809 |
-
|
| 810 |
-
*
|
| 811 |
-
|
| 812 |
-
|
| 813 |
-
|
| 814 |
-
*exception-specification*.
|
| 815 |
|
| 816 |
For a class template specialization, a class member template
|
| 817 |
specialization, or a specialization for a class member of a class
|
| 818 |
template, if the specialization is implicitly instantiated because it is
|
| 819 |
referenced from within another template specialization, if the context
|
|
@@ -846,11 +1039,11 @@ specialization that has a point of instantiation within the translation
|
|
| 846 |
unit, the end of the translation unit is also considered a point of
|
| 847 |
instantiation. A specialization for a class template has at most one
|
| 848 |
point of instantiation within a translation unit. A specialization for
|
| 849 |
any template may have points of instantiation in multiple translation
|
| 850 |
units. If two different points of instantiation give a template
|
| 851 |
-
specialization different meanings according to the one
|
| 852 |
[[basic.def.odr]]), the program is ill-formed, no diagnostic required.
|
| 853 |
|
| 854 |
#### Candidate functions <a id="temp.dep.candidate">[[temp.dep.candidate]]</a>
|
| 855 |
|
| 856 |
For a function call where the *postfix-expression* is a dependent name,
|
|
@@ -883,20 +1076,23 @@ As with non-template classes, the names of namespace-scope friend
|
|
| 883 |
functions of a class template specialization are not visible during an
|
| 884 |
ordinary lookup unless explicitly declared at namespace scope (
|
| 885 |
[[class.friend]]). Such names may be found under the rules for
|
| 886 |
associated classes ([[basic.lookup.argdep]]).[^6]
|
| 887 |
|
|
|
|
|
|
|
| 888 |
``` cpp
|
| 889 |
template<typename T> struct number {
|
| 890 |
number(int);
|
| 891 |
friend number gcd(number x, number y) { return 0; };
|
| 892 |
};
|
| 893 |
|
| 894 |
void g() {
|
| 895 |
number<double> a(3), b(4);
|
| 896 |
-
a = gcd(a,b); // finds gcd because number<double> is an
|
| 897 |
-
//
|
| 898 |
-
// in its namespace (global scope)
|
| 899 |
b = gcd(3,4); // ill-formed; gcd is not visible
|
| 900 |
}
|
| 901 |
```
|
| 902 |
|
|
|
|
|
|
|
|
|
| 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
|
| 13 |
the keyword `typename`.
|
| 14 |
|
| 15 |
+
[*Example 1*:
|
| 16 |
+
|
| 17 |
``` cpp
|
| 18 |
// no B declared here
|
| 19 |
|
| 20 |
class X;
|
| 21 |
|
|
|
|
| 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; // T::A is not a type name:
|
| 34 |
+
// multiplication of T::A by a7; ill-formed, no visible declaration of a7
|
|
|
|
| 35 |
B* a8; // B is not a type name:
|
| 36 |
+
// multiplication of B by a8; ill-formed, no visible declarations of B and a8
|
|
|
|
| 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 |
'typename' nested-name-specifier identifier
|
| 53 |
'typename' nested-name-specifier 'template'ₒₚₜ simple-template-id
|
| 54 |
```
|
| 55 |
|
| 56 |
If a specialization of a template is instantiated for a set of
|
| 57 |
*template-argument*s such that the *qualified-id* prefixed by `typename`
|
| 58 |
+
does not denote a type or a class template, the specialization is
|
| 59 |
+
ill-formed. The usual qualified name lookup ([[basic.lookup.qual]]) is
|
| 60 |
+
used to find the *qualified-id* even in the presence of `typename`.
|
| 61 |
+
|
| 62 |
+
[*Example 2*:
|
| 63 |
|
| 64 |
``` cpp
|
| 65 |
struct A {
|
| 66 |
struct X { };
|
| 67 |
int X;
|
|
|
|
| 78 |
f(b); // OK: T::X refers to B::X
|
| 79 |
f(a); // error: T::X refers to the data member A::X not the struct A::X
|
| 80 |
}
|
| 81 |
```
|
| 82 |
|
| 83 |
+
— *end example*]
|
| 84 |
+
|
| 85 |
+
A qualified name used as the name in a *class-or-decltype* (Clause
|
| 86 |
+
[[class.derived]]) or an *elaborated-type-specifier* is implicitly
|
| 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 |
If, for a given set of template arguments, a specialization of a
|
| 97 |
template is instantiated that refers to a *qualified-id* that denotes a
|
| 98 |
+
type or a class template, and the *qualified-id* refers to a member of
|
| 99 |
+
an unknown specialization, the *qualified-id* shall either be prefixed
|
| 100 |
+
by `typename` or shall be used in a context in which it implicitly names
|
| 101 |
+
a type as described above.
|
| 102 |
+
|
| 103 |
+
[*Example 3*:
|
| 104 |
|
| 105 |
``` cpp
|
| 106 |
template <class T> void f(int i) {
|
| 107 |
T::x * i; // T::x must not be a type
|
| 108 |
}
|
|
|
|
| 119 |
f<Bar>(1); // OK
|
| 120 |
f<Foo>(1); // error: Foo::x is a type
|
| 121 |
}
|
| 122 |
```
|
| 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 the name of a previously
|
| 129 |
+
declared member of the class template that declares a type or a class
|
| 130 |
+
template.
|
| 131 |
+
|
| 132 |
+
[*Note 2*: Such names can be found using unqualified name lookup (
|
| 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 |
Knowing which names are type names allows the syntax of every template
|
| 151 |
+
to be checked. The program is ill-formed, no diagnostic required, if:
|
| 152 |
+
|
| 153 |
+
- no valid specialization can be generated for a template or a
|
| 154 |
+
substatement of a constexpr if statement ([[stmt.if]]) within a
|
| 155 |
+
template and the template is not instantiated, or
|
| 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
|
| 161 |
+
- the interpretation of such a construct in the hypothetical
|
| 162 |
+
instantiation is different from the interpretation of the
|
| 163 |
+
corresponding construct in any actual instantiation of the template.
|
| 164 |
+
\[*Note 3*:
|
| 165 |
+
This can happen in situations including the following:
|
| 166 |
+
- a type used in a non-dependent name is incomplete at the point at
|
| 167 |
+
which a template is defined but is complete at the point at which an
|
| 168 |
+
instantiation is performed, or
|
| 169 |
+
- lookup for a name in the template definition found a
|
| 170 |
+
*using-declaration*, but the lookup in the corresponding scope in
|
| 171 |
+
the instantiation does not find any declarations because the
|
| 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 ([[expr.const]]) within the template
|
| 178 |
+
instantiation uses
|
| 179 |
+
- the value of a `const` object of integral or unscoped enumeration
|
| 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 |
+
|
| 185 |
+
and that entity was not defined when the template was defined, or
|
| 186 |
+
- a class template specialization or variable template specialization
|
| 187 |
+
that is specified by a non-dependent *simple-template-id* is used by
|
| 188 |
+
the template, and either it is instantiated from a partial
|
| 189 |
+
specialization that was not defined when the template was defined or
|
| 190 |
+
it names an explicit specialization that was not declared when the
|
| 191 |
+
template was defined.
|
| 192 |
+
|
| 193 |
+
— *end note*]
|
| 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 International Standard. Exactly
|
| 200 |
+
when these errors are diagnosed is a quality of implementation
|
| 201 |
+
issue. — *end note*]
|
| 202 |
+
|
| 203 |
+
[*Example 5*:
|
| 204 |
|
| 205 |
``` cpp
|
| 206 |
int j;
|
| 207 |
template<class T> class X {
|
| 208 |
void f(T t, int i, char* p) {
|
| 209 |
+
t = i; // diagnosed if X::f is instantiated, and the assignment to t is an error
|
| 210 |
+
p = i; // may be diagnosed even if X::f is not instantiated
|
| 211 |
+
p = j; // may be diagnosed even if X::f is not instantiated
|
|
|
|
|
|
|
|
|
|
| 212 |
}
|
| 213 |
void g(T t) {
|
| 214 |
+
+; // may be diagnosed even if X::g is not instantiated
|
|
|
|
| 215 |
}
|
| 216 |
};
|
| 217 |
|
| 218 |
template<class... T> struct A {
|
| 219 |
void operator++(int, T... t); // error: too many parameters
|
| 220 |
};
|
| 221 |
template<class... T> union X : T... { }; // error: union with base class
|
| 222 |
template<class... T> struct A : T..., T... { }; // error: duplicate base class
|
| 223 |
```
|
| 224 |
|
| 225 |
+
— *end example*]
|
| 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 ([[temp.dep]]).
|
| 232 |
|
| 233 |
+
[*Example 6*:
|
| 234 |
+
|
| 235 |
``` cpp
|
| 236 |
#include <iostream>
|
| 237 |
using namespace std;
|
| 238 |
|
| 239 |
template<class T> class Set {
|
|
|
|
| 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 ([[temp.dep]]).
|
| 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 7*:
|
| 272 |
+
|
| 273 |
``` cpp
|
| 274 |
void f(char);
|
| 275 |
|
| 276 |
template<class T> void g(T t) {
|
| 277 |
f(1); // f(char)
|
| 278 |
f(T(1)); // dependent
|
| 279 |
f(t); // dependent
|
| 280 |
+
dd++; // not dependent; error: declaration for dd not found
|
|
|
|
| 281 |
}
|
| 282 |
|
| 283 |
enum E { e };
|
| 284 |
void f(E);
|
| 285 |
|
| 286 |
double dd;
|
| 287 |
void h() {
|
| 288 |
+
g(e); // will cause one call of f(char) followed by two calls of f(E)
|
|
|
|
| 289 |
g('a'); // will cause three calls of f(char)
|
| 290 |
}
|
| 291 |
```
|
| 292 |
|
| 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 ([[temp.decls]]). — *end note*]
|
| 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 (Clause [[class]]). The injected-class-name can be
|
|
|
|
| 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
|
| 316 |
specialization enclosed in `<>`.
|
| 317 |
|
| 318 |
+
[*Example 1*:
|
| 319 |
+
|
| 320 |
``` cpp
|
| 321 |
template<template<class> class T> class A { };
|
| 322 |
template<class T> class Y;
|
| 323 |
template<> class Y<int> {
|
| 324 |
Y* p; // meaning Y<int>
|
|
|
|
| 328 |
template<class> friend class Y; // meaning ::Y
|
| 329 |
};
|
| 330 |
};
|
| 331 |
```
|
| 332 |
|
| 333 |
+
— *end example*]
|
| 334 |
+
|
| 335 |
The injected-class-name of a class template or class template
|
| 336 |
specialization can be used either as a *template-name* or a *type-name*
|
| 337 |
wherever it is in scope.
|
| 338 |
|
| 339 |
+
[*Example 2*:
|
| 340 |
+
|
| 341 |
``` cpp
|
| 342 |
template <class T> struct Base {
|
| 343 |
Base* p;
|
| 344 |
};
|
| 345 |
|
|
|
|
| 349 |
|
| 350 |
template<class T, template<class> class U = T::template Base> struct Third { };
|
| 351 |
Third<Base<int> > t; // OK: default argument uses injected-class-name as a template
|
| 352 |
```
|
| 353 |
|
| 354 |
+
— *end example*]
|
| 355 |
+
|
| 356 |
A lookup that finds an injected-class-name ([[class.member.lookup]])
|
| 357 |
can result in an ambiguity in certain cases (for example, if it is found
|
| 358 |
in more than one base class). If all of the injected-class-names that
|
| 359 |
are found refer to specializations of the same class template, and if
|
| 360 |
the name is used as a *template-name*, the reference refers to the class
|
| 361 |
template itself and not a specialization thereof, and is not ambiguous.
|
| 362 |
|
| 363 |
+
[*Example 3*:
|
| 364 |
+
|
| 365 |
``` cpp
|
| 366 |
template <class T> struct Base { };
|
| 367 |
template <class T> struct Derived: Base<int>, Base<char> {
|
| 368 |
typename Derived::Base b; // error: ambiguous
|
| 369 |
typename Derived::Base<double> d; // OK
|
| 370 |
};
|
| 371 |
```
|
| 372 |
|
| 373 |
+
— *end example*]
|
| 374 |
+
|
| 375 |
When the normal name of the template (i.e., the name from the enclosing
|
| 376 |
scope, not the injected-class-name) is used, it always refers to the
|
| 377 |
class template itself and not a specialization of the template.
|
| 378 |
|
| 379 |
+
[*Example 4*:
|
| 380 |
+
|
| 381 |
``` cpp
|
| 382 |
template<class T> class X {
|
| 383 |
X* p; // meaning X<T>
|
| 384 |
X<T>* p2;
|
| 385 |
X<int>* p3;
|
| 386 |
::X* p4; // error: missing template argument list
|
| 387 |
// ::X does not refer to the injected-class-name
|
| 388 |
};
|
| 389 |
```
|
| 390 |
|
| 391 |
+
— *end example*]
|
| 392 |
+
|
| 393 |
A *template-parameter* shall not be redeclared within its scope
|
| 394 |
(including nested scopes). A *template-parameter* shall not have the
|
| 395 |
same name as the template name.
|
| 396 |
|
| 397 |
+
[*Example 5*:
|
| 398 |
+
|
| 399 |
``` cpp
|
| 400 |
template<class T, int i> class Y {
|
| 401 |
int T; // error: template-parameter redeclared
|
| 402 |
void f() {
|
| 403 |
char T; // error: template-parameter redeclared
|
|
|
|
| 405 |
};
|
| 406 |
|
| 407 |
template<class X> class X; // error: template-parameter redeclared
|
| 408 |
```
|
| 409 |
|
| 410 |
+
— *end example*]
|
| 411 |
+
|
| 412 |
In the definition of a member of a class template that appears outside
|
| 413 |
of the class template definition, the name of a member of the class
|
| 414 |
template hides the name of a *template-parameter* of any enclosing class
|
| 415 |
templates (but not a *template-parameter* of the member if the member is
|
| 416 |
a class or function template).
|
| 417 |
|
| 418 |
+
[*Example 6*:
|
| 419 |
+
|
| 420 |
``` cpp
|
| 421 |
template<class T> struct A {
|
| 422 |
+
struct B { ... };
|
| 423 |
typedef void C;
|
| 424 |
void f();
|
| 425 |
template<class U> void g(U);
|
| 426 |
};
|
| 427 |
|
|
|
|
| 433 |
B b; // A's B, not the template parameter
|
| 434 |
C c; // the template parameter C, not A's C
|
| 435 |
}
|
| 436 |
```
|
| 437 |
|
| 438 |
+
— *end example*]
|
| 439 |
+
|
| 440 |
In the definition of a member of a class template that appears outside
|
| 441 |
of the namespace containing the class template definition, the name of a
|
| 442 |
*template-parameter* hides the name of a member of this namespace.
|
| 443 |
|
| 444 |
+
[*Example 7*:
|
| 445 |
+
|
| 446 |
``` cpp
|
| 447 |
namespace N {
|
| 448 |
class C { };
|
| 449 |
template<class T> class B {
|
| 450 |
void f(T);
|
|
|
|
| 453 |
template<class C> void N::B<C>::f(C) {
|
| 454 |
C b; // C is the template parameter, not N::C
|
| 455 |
}
|
| 456 |
```
|
| 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 ([[temp.dep.type]]), if the name of the
|
| 463 |
+
base class or the name of a member of the base class is the same as the
|
| 464 |
+
name of a *template-parameter*, the base class name or member name hides
|
| 465 |
+
the *template-parameter* name ([[basic.scope.hiding]]).
|
| 466 |
+
|
| 467 |
+
[*Example 8*:
|
| 468 |
|
| 469 |
``` cpp
|
| 470 |
struct A {
|
| 471 |
+
struct B { ... };
|
| 472 |
int a;
|
| 473 |
int Y;
|
| 474 |
};
|
| 475 |
|
| 476 |
template<class B, class a> struct X : A {
|
| 477 |
B b; // A's B
|
| 478 |
a b; // error: A's a isn't a type name
|
| 479 |
};
|
| 480 |
```
|
| 481 |
|
| 482 |
+
— *end example*]
|
| 483 |
+
|
| 484 |
### Dependent names <a id="temp.dep">[[temp.dep]]</a>
|
| 485 |
|
| 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 expressions may be *type-dependent* (that is, its type may
|
| 492 |
+
depend on a template parameter) or *value-dependent* (that is, its value
|
| 493 |
+
when evaluated as a constant expression ([[expr.const]]) may depend on
|
| 494 |
+
a template parameter) as described in this subclause. In an expression
|
| 495 |
+
of the form:
|
| 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 ([[temp.dep.expr]]), or
|
| 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. Such names are unbound and are
|
| 509 |
looked up at the point of the template instantiation ([[temp.point]])
|
| 510 |
in both the context of the template definition and the context of the
|
| 511 |
point of instantiation.
|
| 512 |
|
| 513 |
+
[*Example 1*:
|
| 514 |
+
|
| 515 |
``` cpp
|
| 516 |
template<class T> struct X : B<T> {
|
| 517 |
typename T::A* pa;
|
| 518 |
void f(B<T>* pb) {
|
| 519 |
static int i = B<T>::i;
|
|
|
|
| 523 |
```
|
| 524 |
|
| 525 |
the base class name `B<T>`, the type name `T::A`, the names `B<T>::i`
|
| 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 ([[temp.dep.type]]) is not examined during unqualified name
|
| 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 |
|
| 537 |
``` cpp
|
| 538 |
typedef double A;
|
| 539 |
template<class T> class B {
|
| 540 |
typedef int A;
|
|
|
|
| 546 |
|
| 547 |
The type name `A` in the definition of `X<T>` binds to the typedef name
|
| 548 |
defined in the global namespace scope, not to the typedef name defined
|
| 549 |
in the base class `B<T>`.
|
| 550 |
|
| 551 |
+
— *end example*]
|
| 552 |
+
|
| 553 |
+
[*Example 3*:
|
| 554 |
+
|
| 555 |
``` cpp
|
| 556 |
struct A {
|
| 557 |
+
struct B { ... };
|
| 558 |
int a;
|
| 559 |
int Y;
|
| 560 |
};
|
| 561 |
|
| 562 |
int a;
|
| 563 |
|
| 564 |
template<class T> struct Y : T {
|
| 565 |
+
struct B { ... };
|
| 566 |
B b; // The B defined in Y
|
| 567 |
void f(int i) { a = i; } // ::a
|
| 568 |
Y* p; // Y<T>
|
| 569 |
};
|
| 570 |
|
|
|
|
| 572 |
```
|
| 573 |
|
| 574 |
The members `A::B`, `A::a`, and `A::Y` of the template argument `A` do
|
| 575 |
not affect the binding of names in `Y<A>`.
|
| 576 |
|
| 577 |
+
— *end example*]
|
| 578 |
+
|
| 579 |
#### Dependent types <a id="temp.dep.type">[[temp.dep.type]]</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
|
|
|
|
| 612 |
current instantiation. In the case of a non-type template argument, the
|
| 613 |
argument must have been given the value of the template parameter and
|
| 614 |
not an expression in which the template parameter appears as a
|
| 615 |
subexpression.
|
| 616 |
|
| 617 |
+
[*Example 1*:
|
| 618 |
+
|
| 619 |
``` cpp
|
| 620 |
template <class T> class A {
|
| 621 |
A* p1; // A is the current instantiation
|
| 622 |
A<T>* p2; // A<T> is the current instantiation
|
| 623 |
A<T*> p3; // A<T*> is not the current instantiation
|
| 624 |
::A<T>* p4; // ::A<T> is the current instantiation
|
| 625 |
class B {
|
| 626 |
B* p1; // B is the current instantiation
|
| 627 |
A<T>::B* p2; // A<T>::B is the current instantiation
|
| 628 |
+
typename A<T*>::B* p3; // A<T*>::B is not the current instantiation
|
|
|
|
| 629 |
};
|
| 630 |
};
|
| 631 |
|
| 632 |
template <class T> class A<T*> {
|
| 633 |
A<T*>* p1; // A<T*> is 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 1*:
|
| 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*:
|
| 661 |
+
|
| 662 |
+
``` cpp
|
| 663 |
+
template<class T> struct A {
|
| 664 |
+
typedef int M;
|
| 665 |
+
struct B {
|
| 666 |
+
typedef void M;
|
| 667 |
+
struct C;
|
| 668 |
+
};
|
| 669 |
+
};
|
| 670 |
+
|
| 671 |
+
template<class T> struct A<T>::B::C : A<T> {
|
| 672 |
+
M m; // OK, A<T>::M
|
| 673 |
+
};
|
| 674 |
+
```
|
| 675 |
+
|
| 676 |
+
— *end example*]
|
| 677 |
+
|
| 678 |
+
— *end note*]
|
| 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 2*: This can only occur when looking up a
|
| 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 3*: If no such member is found, and the
|
| 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 ([[expr.ref]]) for which the type of the object expression
|
| 696 |
is the current instantiation, and the *id-expression*, when looked
|
| 697 |
up ([[basic.lookup.classref]]), refers to at least one member of a
|
| 698 |
class that is the current instantiation or a non-dependent base class
|
| 699 |
+
thereof. \[*Note 4*: If no such member is found, and the current
|
| 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 |
|
| 705 |
``` cpp
|
| 706 |
template <class T> class A {
|
| 707 |
static const int i = 5;
|
| 708 |
int n1[i]; // i refers to a member of the current instantiation
|
|
|
|
| 714 |
template <class T> int A<T>::f() {
|
| 715 |
return i; // i refers to a member of the current instantiation
|
| 716 |
}
|
| 717 |
```
|
| 718 |
|
| 719 |
+
— *end example*]
|
| 720 |
+
|
| 721 |
A name is a *dependent member of the current instantiation* if it is a
|
| 722 |
member of the current instantiation that, when looked up, refers to at
|
| 723 |
least one member of a class that is the current instantiation.
|
| 724 |
|
| 725 |
A name is a *member of an unknown specialization* if it is
|
|
|
|
| 750 |
current instantiation does not refer to a member of the current
|
| 751 |
instantiation or a member of an unknown specialization, the program is
|
| 752 |
ill-formed even if the template containing the member access expression
|
| 753 |
is not instantiated; no diagnostic required.
|
| 754 |
|
| 755 |
+
[*Example 4*:
|
| 756 |
+
|
| 757 |
``` cpp
|
| 758 |
template<class T> class A {
|
| 759 |
typedef int type;
|
| 760 |
void f() {
|
| 761 |
A<T>::type i; // OK: refers to a member of the current instantiation
|
|
|
|
| 763 |
// a member of an unknown specialization
|
| 764 |
}
|
| 765 |
};
|
| 766 |
```
|
| 767 |
|
| 768 |
+
— *end example*]
|
| 769 |
+
|
| 770 |
If, for a given set of template arguments, a specialization of a
|
| 771 |
template is instantiated that refers to a member of the current
|
| 772 |
instantiation with a *qualified-id* or class member access expression,
|
| 773 |
the name in the *qualified-id* or class member access expression is
|
| 774 |
looked up in the template instantiation context. If the result of this
|
| 775 |
lookup differs from the result of name lookup in the template definition
|
| 776 |
+
context, name lookup is ambiguous.
|
| 777 |
+
|
| 778 |
+
[*Example 5*:
|
| 779 |
+
|
| 780 |
+
``` cpp
|
| 781 |
+
struct A {
|
| 782 |
+
int m;
|
| 783 |
+
};
|
| 784 |
+
|
| 785 |
+
struct B {
|
| 786 |
+
int m;
|
| 787 |
+
};
|
| 788 |
+
|
| 789 |
+
template<typename T>
|
| 790 |
+
struct C : A, T {
|
| 791 |
+
int f() { return this->m; } // finds A::m in the template definition context
|
| 792 |
+
int g() { return m; } // finds A::m in the template definition context
|
| 793 |
+
};
|
| 794 |
+
|
| 795 |
+
template int C<B>::f(); // error: finds both A::m and B::m
|
| 796 |
+
template int C<B>::g(); // OK: transformation to class member access syntax
|
| 797 |
+
// does not occur in the template definition context; see~[class.mfct.non-static]
|
| 798 |
+
```
|
| 799 |
+
|
| 800 |
+
— *end example*]
|
| 801 |
|
| 802 |
A type is dependent if it is
|
| 803 |
|
| 804 |
- a template parameter,
|
| 805 |
- a member of an unknown specialization,
|
| 806 |
- a nested class or enumeration that is a dependent member of the
|
| 807 |
current instantiation,
|
| 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 a template
|
| 814 |
parameter or any of the template arguments is a dependent type or an
|
| 815 |
+
expression that is type-dependent or value-dependent or is a pack
|
| 816 |
+
expansion \[*Note 5*: This includes an injected-class-name (Clause
|
| 817 |
+
[[class]]) of a class template used without a
|
| 818 |
+
*template-argument-list*. — *end note*] , or
|
| 819 |
- denoted by `decltype(`*expression*`)`, where *expression* is
|
| 820 |
type-dependent ([[temp.dep.expr]]).
|
| 821 |
|
| 822 |
+
[*Note 6*: Because typedefs do not introduce new types, but instead
|
| 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>
|
| 828 |
|
| 829 |
Except as described below, an expression is type-dependent if any
|
| 830 |
subexpression is type-dependent.
|
|
|
|
| 836 |
|
| 837 |
An *id-expression* is type-dependent if it contains
|
| 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 ([[dcl.spec.auto]]),
|
| 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 ([[dcl.struct.bind]]) whose *brace-or-equal-initializer*
|
| 849 |
+
is type-dependent,
|
| 850 |
+
- the *identifier* `__func__` ([[dcl.fct.def.general]]), where any
|
| 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 |
|
|
|
|
| 863 |
subexpression is type-dependent:
|
| 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 ([[expr.ref]]) is type-dependent if
|
| 872 |
the expression refers to a member of the current instantiation and the
|
| 873 |
type of the referenced member is dependent, or the class member access
|
| 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
|
| 879 |
+
dependent type that is not the current instantiation, the type of `y` is
|
| 880 |
+
always dependent. If `x` or `xp` refers to a non-dependent type or
|
| 881 |
+
refers to the current instantiation, the type of `y` is the type of the
|
| 882 |
+
class member access expression. — *end note*]
|
| 883 |
+
|
| 884 |
+
A *braced-init-list* is type-dependent if any element is type-dependent
|
| 885 |
+
or is a pack expansion.
|
| 886 |
+
|
| 887 |
+
A *fold-expression* is type-dependent.
|
| 888 |
|
| 889 |
#### Value-dependent expressions <a id="temp.dep.constexpr">[[temp.dep.constexpr]]</a>
|
| 890 |
|
| 891 |
+
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 is a constant with literal type and is initialized with an
|
|
|
|
| 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 ([[expr.const]]) succeeds and the result of the evaluation
|
| 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 |
|
| 929 |
A type *template-argument* is dependent if the type it specifies is
|
| 930 |
dependent.
|
|
|
|
| 944 |
### Non-dependent names <a id="temp.nondep">[[temp.nondep]]</a>
|
| 945 |
|
| 946 |
Non-dependent names used in a template definition are found using the
|
| 947 |
usual name lookup and bound at the point they are used.
|
| 948 |
|
| 949 |
+
[*Example 1*:
|
| 950 |
+
|
| 951 |
``` cpp
|
| 952 |
void g(double);
|
| 953 |
void h();
|
| 954 |
|
| 955 |
template<class T> class Z {
|
| 956 |
public:
|
| 957 |
void f() {
|
| 958 |
g(1); // calls g(double)
|
| 959 |
+
h++; // ill-formed: cannot increment function; this could be diagnosed
|
| 960 |
+
// either here or at the point of instantiation
|
|
|
|
| 961 |
}
|
| 962 |
};
|
| 963 |
|
| 964 |
+
void g(int); // not in scope at the point of the template definition, not considered for the call g(1)
|
|
|
|
| 965 |
```
|
| 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 |
|
|
|
|
| 994 |
in a way which uses the definition of a default argument of that
|
| 995 |
function template or member function, the point of instantiation of the
|
| 996 |
default argument is the point of instantiation of the function template
|
| 997 |
or member function specialization.
|
| 998 |
|
| 999 |
+
For a *noexcept-specifier* of a function template specialization or
|
| 1000 |
+
specialization of a member function of a class template, if the
|
| 1001 |
+
*noexcept-specifier* is implicitly instantiated because it is needed by
|
| 1002 |
+
another template specialization and the context that requires it depends
|
| 1003 |
+
on a template parameter, the point of instantiation of the
|
| 1004 |
+
*noexcept-specifier* is the point of instantiation of the specialization
|
| 1005 |
+
that requires it. Otherwise, the point of instantiation for such a
|
| 1006 |
+
*noexcept-specifier* immediately follows the namespace scope declaration
|
| 1007 |
+
or definition that requires the *noexcept-specifier*.
|
|
|
|
| 1008 |
|
| 1009 |
For a class template specialization, a class member template
|
| 1010 |
specialization, or a specialization for a class member of a class
|
| 1011 |
template, if the specialization is implicitly instantiated because it is
|
| 1012 |
referenced from within another template specialization, if the context
|
|
|
|
| 1039 |
unit, the end of the translation unit is also considered a point of
|
| 1040 |
instantiation. A specialization for a class template has at most one
|
| 1041 |
point of instantiation within a translation unit. A specialization for
|
| 1042 |
any template may have points of instantiation in multiple translation
|
| 1043 |
units. If two different points of instantiation give a template
|
| 1044 |
+
specialization different meanings according to the one-definition rule (
|
| 1045 |
[[basic.def.odr]]), the program is ill-formed, no diagnostic required.
|
| 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,
|
|
|
|
| 1076 |
functions of a class template specialization are not visible during an
|
| 1077 |
ordinary lookup unless explicitly declared at namespace scope (
|
| 1078 |
[[class.friend]]). Such names may be found under the rules for
|
| 1079 |
associated classes ([[basic.lookup.argdep]]).[^6]
|
| 1080 |
|
| 1081 |
+
[*Example 1*:
|
| 1082 |
+
|
| 1083 |
``` cpp
|
| 1084 |
template<typename T> struct number {
|
| 1085 |
number(int);
|
| 1086 |
friend number gcd(number x, number y) { return 0; };
|
| 1087 |
};
|
| 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); // ill-formed; gcd is not visible
|
| 1094 |
}
|
| 1095 |
```
|
| 1096 |
|
| 1097 |
+
— *end example*]
|
| 1098 |
+
|