tmp/tmpwkkey7ey/{from.md → to.md}
RENAMED
|
@@ -39,65 +39,67 @@ specializations instantiated from the class template. Similarly,
|
|
| 39 |
`Array<char*>`; other `Array` types will be sorted by functions
|
| 40 |
generated from the template.
|
| 41 |
|
| 42 |
— *end example*]
|
| 43 |
|
| 44 |
-
|
| 45 |
-
|
|
|
|
| 46 |
|
| 47 |
An explicit specialization may be declared in any scope in which the
|
| 48 |
-
corresponding primary template may be defined
|
| 49 |
-
[[class.mem]], [[temp.mem]]
|
| 50 |
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
|
|
|
| 54 |
|
| 55 |
[*Note 1*: A declaration, but not a definition of the template is
|
| 56 |
required. — *end note*]
|
| 57 |
|
| 58 |
-
The definition of a class or class template shall
|
| 59 |
declaration of an explicit specialization for a member template of the
|
| 60 |
class or class template.
|
| 61 |
|
| 62 |
[*Example 2*:
|
| 63 |
|
| 64 |
``` cpp
|
| 65 |
template<> class X<int> { ... }; // error: X not a template
|
| 66 |
|
| 67 |
template<class T> class X;
|
| 68 |
|
| 69 |
-
template<> class X<char*> { ... }; // OK
|
| 70 |
```
|
| 71 |
|
| 72 |
— *end example*]
|
| 73 |
|
| 74 |
A member function, a member function template, a member class, a member
|
| 75 |
enumeration, a member class template, a static data member, or a static
|
| 76 |
data member template of a class template may be explicitly specialized
|
| 77 |
for a class specialization that is implicitly instantiated; in this
|
| 78 |
-
case, the definition of the class template shall
|
| 79 |
-
specialization for the member of the class template. If such an
|
| 80 |
-
specialization for the member of a class template names an
|
| 81 |
implicitly-declared special member function [[special]], the program is
|
| 82 |
ill-formed.
|
| 83 |
|
| 84 |
A member of an explicitly specialized class is not implicitly
|
| 85 |
instantiated from the member declaration of the class template; instead,
|
| 86 |
the member of the class template specialization shall itself be
|
| 87 |
-
explicitly defined if its definition is required.
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
|
| 100 |
[*Example 3*:
|
| 101 |
|
| 102 |
``` cpp
|
| 103 |
template<class T> struct A {
|
|
@@ -143,20 +145,20 @@ template<class U> void A<short>::C<U>::f() { ... } // error: template<> requi
|
|
| 143 |
```
|
| 144 |
|
| 145 |
— *end example*]
|
| 146 |
|
| 147 |
If a template, a member template or a member of a class template is
|
| 148 |
-
explicitly specialized
|
| 149 |
-
|
| 150 |
-
instantiation to take place, in every translation unit in which
|
| 151 |
-
use occurs; no diagnostic is required. If the program does not
|
| 152 |
-
definition for an explicit specialization and either the
|
| 153 |
-
is used in a way that would cause an implicit
|
| 154 |
-
place or the member is a virtual member function,
|
| 155 |
-
ill-formed, no diagnostic required. An implicit
|
| 156 |
-
generated for an explicit specialization that is
|
| 157 |
-
defined.
|
| 158 |
|
| 159 |
[*Example 4*:
|
| 160 |
|
| 161 |
``` cpp
|
| 162 |
class String { };
|
|
@@ -166,11 +168,11 @@ template<class T> void sort(Array<T>& v) { ... }
|
|
| 166 |
void f(Array<String>& v) {
|
| 167 |
sort(v); // use primary template sort(Array<T>&), T is String
|
| 168 |
}
|
| 169 |
|
| 170 |
template<> void sort<String>(Array<String>& v); // error: specialization after use of primary template
|
| 171 |
-
template<> void sort<>(Array<char*>& v); // OK
|
| 172 |
template<class T> struct A {
|
| 173 |
enum E : T;
|
| 174 |
enum class S : T;
|
| 175 |
};
|
| 176 |
template<> enum A<int>::E : int { eint }; // OK
|
|
@@ -201,52 +203,32 @@ can affect whether a program is well-formed according to the relative
|
|
| 201 |
positioning of the explicit specialization declarations and their points
|
| 202 |
of instantiation in the translation unit as specified above and below.
|
| 203 |
When writing a specialization, be careful about its location; or to make
|
| 204 |
it compile will be such a trial as to kindle its self-immolation.
|
| 205 |
|
| 206 |
-
A template explicit specialization is in the scope of the namespace in
|
| 207 |
-
which the template was defined.
|
| 208 |
-
|
| 209 |
-
[*Example 5*:
|
| 210 |
-
|
| 211 |
-
``` cpp
|
| 212 |
-
namespace N {
|
| 213 |
-
template<class T> class X { ... };
|
| 214 |
-
template<class T> class Y { ... };
|
| 215 |
-
|
| 216 |
-
template<> class X<int> { ... }; // OK: specialization in same namespace
|
| 217 |
-
template<> class Y<double>; // forward-declare intent to specialize for double
|
| 218 |
-
}
|
| 219 |
-
|
| 220 |
-
template<> class N::Y<double> { ... }; // OK: specialization in enclosing namespace
|
| 221 |
-
template<> class N::Y<short> { ... }; // OK: specialization in enclosing namespace
|
| 222 |
-
```
|
| 223 |
-
|
| 224 |
-
— *end example*]
|
| 225 |
-
|
| 226 |
A *simple-template-id* that names a class template explicit
|
| 227 |
specialization that has been declared but not defined can be used
|
| 228 |
exactly like the names of other incompletely-defined classes
|
| 229 |
[[basic.types]].
|
| 230 |
|
| 231 |
-
[*Example
|
| 232 |
|
| 233 |
``` cpp
|
| 234 |
template<class T> class X; // X is a class template
|
| 235 |
template<> class X<int>;
|
| 236 |
|
| 237 |
-
X<int>* p; // OK
|
| 238 |
X<int> x; // error: object of incomplete class X<int>
|
| 239 |
```
|
| 240 |
|
| 241 |
— *end example*]
|
| 242 |
|
| 243 |
A trailing *template-argument* can be left unspecified in the
|
| 244 |
*template-id* naming an explicit function template specialization
|
| 245 |
-
provided it can be deduced
|
| 246 |
|
| 247 |
-
[*Example
|
| 248 |
|
| 249 |
``` cpp
|
| 250 |
template<class T> class Array { ... };
|
| 251 |
template<class T> void sort(Array<T>& v);
|
| 252 |
|
|
@@ -269,22 +251,29 @@ arguments are left unspecified. — *end note*]
|
|
| 269 |
A function with the same name as a template and a type that exactly
|
| 270 |
matches that of a template specialization is not an explicit
|
| 271 |
specialization [[temp.fct]].
|
| 272 |
|
| 273 |
Whether an explicit specialization of a function or variable template is
|
| 274 |
-
inline, constexpr, or
|
| 275 |
-
|
| 276 |
-
template
|
|
|
|
| 277 |
|
| 278 |
-
[*Example
|
| 279 |
|
| 280 |
``` cpp
|
| 281 |
template<class T> void f(T) { ... }
|
| 282 |
template<class T> inline T g(T) { ... }
|
| 283 |
|
| 284 |
-
template<> inline void f<>(int) { ... } // OK
|
| 285 |
-
template<> int g<>(int) { ... } // OK
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 286 |
```
|
| 287 |
|
| 288 |
— *end example*]
|
| 289 |
|
| 290 |
An explicit specialization of a static data member of a template or an
|
|
@@ -292,28 +281,29 @@ explicit specialization of a static data member template is a definition
|
|
| 292 |
if the declaration includes an initializer; otherwise, it is a
|
| 293 |
declaration.
|
| 294 |
|
| 295 |
[*Note 3*:
|
| 296 |
|
| 297 |
-
The definition of a static data member of a template
|
| 298 |
-
default-initialization
|
|
|
|
| 299 |
|
| 300 |
``` cpp
|
| 301 |
template<> X Q<int>::x; // declaration
|
| 302 |
template<> X Q<int>::x (); // error: declares a function
|
| 303 |
-
template<> X Q<int>::x
|
| 304 |
```
|
| 305 |
|
| 306 |
— *end note*]
|
| 307 |
|
| 308 |
A member or a member template of a class template may be explicitly
|
| 309 |
specialized for a given implicit instantiation of the class template,
|
| 310 |
even if the member or member template is defined in the class template
|
| 311 |
definition. An explicit specialization of a member or member template is
|
| 312 |
specified using the syntax for explicit specialization.
|
| 313 |
|
| 314 |
-
[*Example
|
| 315 |
|
| 316 |
``` cpp
|
| 317 |
template<class T> struct A {
|
| 318 |
void f(T);
|
| 319 |
template<class X1> void g1(T, X1);
|
|
@@ -345,11 +335,11 @@ template<> void A<int>::h(int) { }
|
|
| 345 |
A member or a member template may be nested within many enclosing class
|
| 346 |
templates. In an explicit specialization for such a member, the member
|
| 347 |
declaration shall be preceded by a `template<>` for each enclosing class
|
| 348 |
template that is explicitly specialized.
|
| 349 |
|
| 350 |
-
[*Example
|
| 351 |
|
| 352 |
``` cpp
|
| 353 |
template<class T1> class A {
|
| 354 |
template<class T2> class B {
|
| 355 |
void mf();
|
|
@@ -371,11 +361,11 @@ declaration, the keyword `template` followed by a
|
|
| 371 |
*template-parameter-list* shall be provided instead of the `template<>`
|
| 372 |
preceding the explicit specialization declaration of the member. The
|
| 373 |
types of the *template-parameter*s in the *template-parameter-list*
|
| 374 |
shall be the same as those specified in the primary template definition.
|
| 375 |
|
| 376 |
-
[*Example
|
| 377 |
|
| 378 |
``` cpp
|
| 379 |
template <class T1> class A {
|
| 380 |
template<class T2> class B {
|
| 381 |
template<class T3> void mf1(T3);
|
|
@@ -408,9 +398,9 @@ definition for one of the following explicit specializations:
|
|
| 408 |
- the explicit specialization of a function template;
|
| 409 |
- the explicit specialization of a member function template;
|
| 410 |
- the explicit specialization of a member function of a class template
|
| 411 |
where the class template specialization to which the member function
|
| 412 |
specialization belongs is implicitly instantiated. \[*Note 4*: Default
|
| 413 |
-
function arguments
|
| 414 |
of a member function of a class template specialization that is
|
| 415 |
explicitly specialized. — *end note*]
|
| 416 |
|
|
|
|
| 39 |
`Array<char*>`; other `Array` types will be sorted by functions
|
| 40 |
generated from the template.
|
| 41 |
|
| 42 |
— *end example*]
|
| 43 |
|
| 44 |
+
The *declaration* in an *explicit-specialization* shall not be an
|
| 45 |
+
*export-declaration*. An explicit specialization shall not use a
|
| 46 |
+
*storage-class-specifier* [[dcl.stc]] other than `thread_local`.
|
| 47 |
|
| 48 |
An explicit specialization may be declared in any scope in which the
|
| 49 |
+
corresponding primary template may be defined
|
| 50 |
+
[[dcl.meaning]], [[class.mem]], [[temp.mem]].
|
| 51 |
|
| 52 |
+
An explicit specialization does not introduce a name
|
| 53 |
+
[[basic.scope.scope]]. A declaration of a function template, class
|
| 54 |
+
template, or variable template being explicitly specialized shall be
|
| 55 |
+
reachable from the declaration of the explicit specialization.
|
| 56 |
|
| 57 |
[*Note 1*: A declaration, but not a definition of the template is
|
| 58 |
required. — *end note*]
|
| 59 |
|
| 60 |
+
The definition of a class or class template shall be reachable from the
|
| 61 |
declaration of an explicit specialization for a member template of the
|
| 62 |
class or class template.
|
| 63 |
|
| 64 |
[*Example 2*:
|
| 65 |
|
| 66 |
``` cpp
|
| 67 |
template<> class X<int> { ... }; // error: X not a template
|
| 68 |
|
| 69 |
template<class T> class X;
|
| 70 |
|
| 71 |
+
template<> class X<char*> { ... }; // OK, X is a template
|
| 72 |
```
|
| 73 |
|
| 74 |
— *end example*]
|
| 75 |
|
| 76 |
A member function, a member function template, a member class, a member
|
| 77 |
enumeration, a member class template, a static data member, or a static
|
| 78 |
data member template of a class template may be explicitly specialized
|
| 79 |
for a class specialization that is implicitly instantiated; in this
|
| 80 |
+
case, the definition of the class template shall be reachable from the
|
| 81 |
+
explicit specialization for the member of the class template. If such an
|
| 82 |
+
explicit specialization for the member of a class template names an
|
| 83 |
implicitly-declared special member function [[special]], the program is
|
| 84 |
ill-formed.
|
| 85 |
|
| 86 |
A member of an explicitly specialized class is not implicitly
|
| 87 |
instantiated from the member declaration of the class template; instead,
|
| 88 |
the member of the class template specialization shall itself be
|
| 89 |
+
explicitly defined if its definition is required. The definition of the
|
| 90 |
+
class template explicit specialization shall be reachable from the
|
| 91 |
+
definition of any member of it. The definition of an explicitly
|
| 92 |
+
specialized class is unrelated to the definition of a generated
|
| 93 |
+
specialization. That is, its members need not have the same names,
|
| 94 |
+
types, etc. as the members of a generated specialization. Members of an
|
| 95 |
+
explicitly specialized class template are defined in the same manner as
|
| 96 |
+
members of normal classes, and not using the `template<>` syntax. The
|
| 97 |
+
same is true when defining a member of an explicitly specialized member
|
| 98 |
+
class. However, `template<>` is used in defining a member of an
|
| 99 |
+
explicitly specialized member class template that is specialized as a
|
| 100 |
+
class template.
|
| 101 |
|
| 102 |
[*Example 3*:
|
| 103 |
|
| 104 |
``` cpp
|
| 105 |
template<class T> struct A {
|
|
|
|
| 145 |
```
|
| 146 |
|
| 147 |
— *end example*]
|
| 148 |
|
| 149 |
If a template, a member template or a member of a class template is
|
| 150 |
+
explicitly specialized, a declaration of that specialization shall be
|
| 151 |
+
reachable from every use of that specialization that would cause an
|
| 152 |
+
implicit instantiation to take place, in every translation unit in which
|
| 153 |
+
such a use occurs; no diagnostic is required. If the program does not
|
| 154 |
+
provide a definition for an explicit specialization and either the
|
| 155 |
+
specialization is used in a way that would cause an implicit
|
| 156 |
+
instantiation to take place or the member is a virtual member function,
|
| 157 |
+
the program is ill-formed, no diagnostic required. An implicit
|
| 158 |
+
instantiation is never generated for an explicit specialization that is
|
| 159 |
+
declared but not defined.
|
| 160 |
|
| 161 |
[*Example 4*:
|
| 162 |
|
| 163 |
``` cpp
|
| 164 |
class String { };
|
|
|
|
| 168 |
void f(Array<String>& v) {
|
| 169 |
sort(v); // use primary template sort(Array<T>&), T is String
|
| 170 |
}
|
| 171 |
|
| 172 |
template<> void sort<String>(Array<String>& v); // error: specialization after use of primary template
|
| 173 |
+
template<> void sort<>(Array<char*>& v); // OK, sort<char*> not yet used
|
| 174 |
template<class T> struct A {
|
| 175 |
enum E : T;
|
| 176 |
enum class S : T;
|
| 177 |
};
|
| 178 |
template<> enum A<int>::E : int { eint }; // OK
|
|
|
|
| 203 |
positioning of the explicit specialization declarations and their points
|
| 204 |
of instantiation in the translation unit as specified above and below.
|
| 205 |
When writing a specialization, be careful about its location; or to make
|
| 206 |
it compile will be such a trial as to kindle its self-immolation.
|
| 207 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 208 |
A *simple-template-id* that names a class template explicit
|
| 209 |
specialization that has been declared but not defined can be used
|
| 210 |
exactly like the names of other incompletely-defined classes
|
| 211 |
[[basic.types]].
|
| 212 |
|
| 213 |
+
[*Example 5*:
|
| 214 |
|
| 215 |
``` cpp
|
| 216 |
template<class T> class X; // X is a class template
|
| 217 |
template<> class X<int>;
|
| 218 |
|
| 219 |
+
X<int>* p; // OK, pointer to declared class X<int>
|
| 220 |
X<int> x; // error: object of incomplete class X<int>
|
| 221 |
```
|
| 222 |
|
| 223 |
— *end example*]
|
| 224 |
|
| 225 |
A trailing *template-argument* can be left unspecified in the
|
| 226 |
*template-id* naming an explicit function template specialization
|
| 227 |
+
provided it can be deduced [[temp.deduct.decl]].
|
| 228 |
|
| 229 |
+
[*Example 6*:
|
| 230 |
|
| 231 |
``` cpp
|
| 232 |
template<class T> class Array { ... };
|
| 233 |
template<class T> void sort(Array<T>& v);
|
| 234 |
|
|
|
|
| 251 |
A function with the same name as a template and a type that exactly
|
| 252 |
matches that of a template specialization is not an explicit
|
| 253 |
specialization [[temp.fct]].
|
| 254 |
|
| 255 |
Whether an explicit specialization of a function or variable template is
|
| 256 |
+
inline, constexpr, constinit, or consteval is determined by the explicit
|
| 257 |
+
specialization and is independent of those properties of the template.
|
| 258 |
+
Similarly, attributes appearing in the declaration of a template have no
|
| 259 |
+
effect on an explicit specialization of that template.
|
| 260 |
|
| 261 |
+
[*Example 7*:
|
| 262 |
|
| 263 |
``` cpp
|
| 264 |
template<class T> void f(T) { ... }
|
| 265 |
template<class T> inline T g(T) { ... }
|
| 266 |
|
| 267 |
+
template<> inline void f<>(int) { ... } // OK, inline
|
| 268 |
+
template<> int g<>(int) { ... } // OK, not inline
|
| 269 |
+
|
| 270 |
+
template<typename> [[noreturn]] void h([[maybe_unused]] int i);
|
| 271 |
+
template<> void h<int>(int i) {
|
| 272 |
+
// Implementations are expected not to warn that the function returns
|
| 273 |
+
// but can warn about the unused parameter.
|
| 274 |
+
}
|
| 275 |
```
|
| 276 |
|
| 277 |
— *end example*]
|
| 278 |
|
| 279 |
An explicit specialization of a static data member of a template or an
|
|
|
|
| 281 |
if the declaration includes an initializer; otherwise, it is a
|
| 282 |
declaration.
|
| 283 |
|
| 284 |
[*Note 3*:
|
| 285 |
|
| 286 |
+
The definition of a static data member of a template for which
|
| 287 |
+
default-initialization is desired can use functional cast notation
|
| 288 |
+
[[expr.type.conv]]:
|
| 289 |
|
| 290 |
``` cpp
|
| 291 |
template<> X Q<int>::x; // declaration
|
| 292 |
template<> X Q<int>::x (); // error: declares a function
|
| 293 |
+
template<> X Q<int>::x = X(); // definition
|
| 294 |
```
|
| 295 |
|
| 296 |
— *end note*]
|
| 297 |
|
| 298 |
A member or a member template of a class template may be explicitly
|
| 299 |
specialized for a given implicit instantiation of the class template,
|
| 300 |
even if the member or member template is defined in the class template
|
| 301 |
definition. An explicit specialization of a member or member template is
|
| 302 |
specified using the syntax for explicit specialization.
|
| 303 |
|
| 304 |
+
[*Example 8*:
|
| 305 |
|
| 306 |
``` cpp
|
| 307 |
template<class T> struct A {
|
| 308 |
void f(T);
|
| 309 |
template<class X1> void g1(T, X1);
|
|
|
|
| 335 |
A member or a member template may be nested within many enclosing class
|
| 336 |
templates. In an explicit specialization for such a member, the member
|
| 337 |
declaration shall be preceded by a `template<>` for each enclosing class
|
| 338 |
template that is explicitly specialized.
|
| 339 |
|
| 340 |
+
[*Example 9*:
|
| 341 |
|
| 342 |
``` cpp
|
| 343 |
template<class T1> class A {
|
| 344 |
template<class T2> class B {
|
| 345 |
void mf();
|
|
|
|
| 361 |
*template-parameter-list* shall be provided instead of the `template<>`
|
| 362 |
preceding the explicit specialization declaration of the member. The
|
| 363 |
types of the *template-parameter*s in the *template-parameter-list*
|
| 364 |
shall be the same as those specified in the primary template definition.
|
| 365 |
|
| 366 |
+
[*Example 10*:
|
| 367 |
|
| 368 |
``` cpp
|
| 369 |
template <class T1> class A {
|
| 370 |
template<class T2> class B {
|
| 371 |
template<class T3> void mf1(T3);
|
|
|
|
| 398 |
- the explicit specialization of a function template;
|
| 399 |
- the explicit specialization of a member function template;
|
| 400 |
- the explicit specialization of a member function of a class template
|
| 401 |
where the class template specialization to which the member function
|
| 402 |
specialization belongs is implicitly instantiated. \[*Note 4*: Default
|
| 403 |
+
function arguments can be specified in the declaration or definition
|
| 404 |
of a member function of a class template specialization that is
|
| 405 |
explicitly specialized. — *end note*]
|
| 406 |
|