forked from cplusplus/draft
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplates.tex
8908 lines (7882 loc) · 291 KB
/
templates.tex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
%!TEX root = std.tex
\rSec0[temp]{Templates}%
\indextext{template|(}
\gramSec[gram.temp]{Templates}
\indextext{parameterized type|see{template}}%
\indextext{type generator|see{template}}
\pnum
A \defn{template} defines a family of classes, functions, or variables,
an alias for a family of types, or a concept.
\indextext{\idxcode{template}}%
%
\begin{bnf}
\nontermdef{template-declaration}\br
template-head declaration\br
template-head concept-definition
\end{bnf}
\begin{bnf}
\nontermdef{template-head}\br
\terminal{template} \terminal{<} template-parameter-list \terminal{>} \opt{requires-clause}
\end{bnf}
\begin{bnf}
\nontermdef{template-parameter-list}\br
template-parameter\br
template-parameter-list \terminal{,} template-parameter
\end{bnf}
\begin{bnf}
\nontermdef{requires-clause}\br
\terminal{requires} constraint-logical-or-expression
\end{bnf}
\begin{bnf}
\nontermdef{constraint-logical-or-expression}\br
constraint-logical-and-expression\br
constraint-logical-or-expression \terminal{||} constraint-logical-and-expression
\end{bnf}
\begin{bnf}
\nontermdef{constraint-logical-and-expression}\br
primary-expression\br
constraint-logical-and-expression \terminal{\&\&} primary-expression
\end{bnf}
\begin{bnf}
\nontermdef{concept-definition}\br
\terminal{concept} concept-name \terminal{=} constraint-expression \terminal{;}
\end{bnf}
\begin{bnf}
\nontermdef{concept-name}\br
identifier
\end{bnf}
\begin{note} The \tcode{>} token following the
\grammarterm{template-parameter-list} of a
\grammarterm{template-declaration}
may be the product of replacing a
\tcode{>{>}} token by two consecutive \tcode{>}
tokens\iref{temp.names}.\end{note}
\pnum
The
\grammarterm{declaration}
in a
\grammarterm{template-declaration}
(if any)
shall
\begin{itemize}
\item declare or define a function, a class, or a variable, or
\item define a member function, a member class, a member enumeration, or a static data member of a
class template or of a class nested within a class template, or
\item define a member template of a class or class template, or
\item be a \grammarterm{deduction-guide}, or
\item be an \grammarterm{alias-declaration}.
\end{itemize}
\pnum
A \grammarterm{template-declaration} is a \grammarterm{declaration}.
\indextext{template!definition of}%
A \grammarterm{template-declaration} is also a definition
if its
\grammarterm{template-head} is followed by
either a \grammarterm{concept-definition} or
a \grammarterm{declaration} that
defines a function, a class, a variable, or a
static data member. A declaration introduced by a template declaration of a
\indextext{variable template!definition of}%
variable is a \defnx{variable template}{template!variable}. A variable template at class scope is a
\defnx{static data member template}{template!static data member}.
\begin{example}
\begin{codeblock}
template<class T>
constexpr T pi = T(3.1415926535897932385L);
template<class T>
T circular_area(T r) {
return pi<T> * r * r;
}
struct matrix_constants {
template<class T>
using pauli = hermitian_matrix<T, 2>;
template<class T>
constexpr pauli<T> sigma1 = { { 0, 1 }, { 1, 0 } };
template<class T>
constexpr pauli<T> sigma2 = { { 0, -1i }, { 1i, 0 } };
template<class T>
constexpr pauli<T> sigma3 = { { 1, 0 }, { 0, -1 } };
};
\end{codeblock}
\end{example}
\pnum
A
\grammarterm{template-declaration}
can appear only as a namespace scope or class scope declaration.
In a function template declaration, the last component of the
\grammarterm{declarator-id}
shall not be a
\grammarterm{template-id}.
\begin{note}
That last component may be an \grammarterm{identifier}, an \grammarterm{operator-function-id},
a \grammarterm{conversion-function-id}, or a \grammarterm{literal-operator-id}. In
a class template declaration, if the
class name
is a
\grammarterm{simple-template-id},
the declaration declares a class template partial specialization\iref{temp.class.spec}.
\end{note}
\pnum
In a
\grammarterm{template-declaration},
explicit specialization, or explicit instantiation the
\grammarterm{init-declarator-list}
in the declaration shall contain at most one declarator.
When such a declaration is used to declare a class template,
no declarator is permitted.
\pnum
\indextext{template name!linkage of}%
A template name has linkage\iref{basic.link}.
Specializations (explicit or implicit) of
a template that has internal linkage are
distinct from all specializations in other translation
units.
A template, a template explicit specialization\iref{temp.expl.spec}, and a class
template partial specialization shall not have C linkage. Use of a linkage specification
other than \tcode{"C"} or \tcode{"C++"} with any of these constructs is
conditionally-supported, with
\impldef{semantics of linkage specification on templates} semantics.
Template definitions shall obey the one-definition rule\iref{basic.def.odr}.
\begin{note}
Default arguments for function templates and for member functions of
class templates are considered definitions for the purpose of template
instantiation\iref{temp.decls} and must also obey the one-definition rule.
\end{note}
\pnum
A class template shall not have the same name as any other
template, class, function, variable, enumeration, enumerator, namespace, or
type in the same scope\iref{basic.scope}, except as specified in~\ref{temp.class.spec}.
Except that a function template can be overloaded either by non-template
functions\iref{dcl.fct} with the same name or by other function templates
with the same name\iref{temp.over},
a template name declared in namespace scope or in class scope shall be unique
in that scope.
\pnum
\indextext{entity!templated}%
A \defn{templated entity} is
\begin{itemize}
\item a template,
\item an entity defined\iref{basic.def} or created\iref{class.temporary}
in a templated entity,
\item a member of a templated entity,
\item an enumerator for an enumeration that is a templated entity, or
\item the closure type of a \grammarterm{lambda-expression}\iref{expr.prim.lambda.closure}
appearing in the declaration of a templated entity.
\end{itemize}
\begin{note}
A local class, a local variable, or a friend function defined in a
templated entity is a templated entity.
\end{note}
\pnum
A \grammarterm{template-declaration} is written
in terms of its template parameters.
The optional \grammarterm{requires-clause} following a
\grammarterm{template-parameter-list} allows the specification of
constraints\iref{temp.constr.decl} on template arguments\iref{temp.arg}.
The \grammarterm{requires-clause} introduces the
\grammarterm{constraint-expression} that results from interpreting
the \grammarterm{constraint-logical-or-expression} as a
\grammarterm{constraint-expression}.
The \grammarterm{constraint-logical-or-expression} of a
\grammarterm{requires-clause} is an unevaluated operand\iref{expr}.
\begin{note}
The expression in a \grammarterm{requires-clause}
uses a restricted grammar to avoid ambiguities.
Parentheses can be used to specify arbitrary expressions
in a \grammarterm{requires-clause}.
\begin{example}
\begin{codeblock}
template<int N> requires N == sizeof new unsigned short
int f(); // error: parentheses required around \tcode{==} expression
\end{codeblock}
\end{example}
\end{note}
\pnum
A function template, member function of a class template, variable template,
or static data
member of a class template shall be defined in every translation unit in
which it is implicitly instantiated\iref{temp.inst} unless the
corresponding specialization is explicitly instantiated\iref{temp.explicit}
in some translation unit; no diagnostic is required.
\rSec1[temp.param]{Template parameters}
\pnum
The syntax for
\grammarterm{template-parameter}{s}
is:
\begin{bnf}
\nontermdef{template-parameter}\br
type-parameter\br
parameter-declaration\br
constrained-parameter
\end{bnf}
\begin{bnf}
\nontermdef{type-parameter}\br
type-parameter-key \opt{\terminal{...}} \opt{identifier}\br
type-parameter-key \opt{identifier} \terminal{=} type-id\br
template-head type-parameter-key \opt{\terminal{...}} \opt{identifier}\br
template-head type-parameter-key \opt{identifier} \terminal{=} id-expression
\end{bnf}
\begin{bnf}
\nontermdef{type-parameter-key}\br
\terminal{class}\br
\terminal{typename}
\end{bnf}
\begin{bnf}
\nontermdef{constrained-parameter}\br
qualified-concept-name \terminal{...} \opt{identifier}\br
qualified-concept-name \opt{identifier} \opt{default-template-argument}
\end{bnf}
\begin{bnf}
\nontermdef{qualified-concept-name}\br
\opt{nested-name-specifier} concept-name\br
\opt{nested-name-specifier} partial-concept-id
\end{bnf}
\begin{bnf}
\nontermdef{partial-concept-id}\br
concept-name \terminal{<} \opt{template-argument-list} \terminal{>}
\end{bnf}
\begin{bnf}
\nontermdef{default-template-argument}\br
\terminal{=} type-id\br
\terminal{=} id-expression\br
\terminal{=} initializer-clause
\end{bnf}
\begin{note} The \tcode{>} token following the
\grammarterm{template-parameter-list} of a
\grammarterm{type-parameter}
may be the product of replacing a
\tcode{>{>}} token by two consecutive \tcode{>}
tokens\iref{temp.names}.\end{note}
\pnum
There is no semantic difference between
\tcode{class}
and
\tcode{typename}
in a
\grammarterm{type-parameter-key}.
\tcode{typename}
followed by an
\grammarterm{unqualified-id}
names a template type parameter.
\tcode{typename}
followed by a
\grammarterm{qualified-id}
denotes the type in a non-type%
\footnote{Since template
\grammarterm{template-parameter}{s}
and template
\grammarterm{template-argument}{s}
are treated as types for descriptive purposes, the terms
\term{non-type parameter}
and
\term{non-type argument}
are used to refer to non-type, non-template parameters and arguments.}
\grammarterm{parameter-declaration}.
A \grammarterm{template-parameter} of the form
\tcode{class} \grammarterm{identifier} is a \grammarterm{type-parameter}.
\begin{example}
\begin{codeblock}
class T { @\commentellip@ };
int i;
template<class T, T i> void f(T t) {
T t1 = i; // template-parameters \tcode{T} and \tcode{i}
::T t2 = ::i; // global namespace members \tcode{T} and \tcode{i}
}
\end{codeblock}
Here, the template \tcode{f} has a \grammarterm{type-parameter}
called \tcode{T}, rather than an unnamed non-type
\grammarterm{template-parameter} of class \tcode{T}.
\end{example}
A storage class shall not be specified in a
\grammarterm{template-parameter}
declaration.
Types shall not be defined in a \grammarterm{template-parameter}
declaration.
\pnum
A
\grammarterm{type-parameter}
whose identifier does not follow an ellipsis
defines its
\grammarterm{identifier}
to be a
\grammarterm{typedef-name}
(if declared without
\tcode{template})
or
\grammarterm{template-name}
(if declared with
\tcode{template})
in the scope of the template declaration.
\begin{note}
A template argument may be a class template or alias template.
For example,
\begin{codeblock}
template<class T> class myarray { @\commentellip@ };
template<class K, class V, template<class T> class C = myarray>
class Map {
C<K> key;
C<V> value;
};
\end{codeblock}
\end{note}
\pnum
A non-type
\grammarterm{template-parameter}
shall have one of the following (optionally
cv-qualified)
types:
\begin{itemize}
\item integral or enumeration type,
\item pointer to object or pointer to function,
\item lvalue reference to object or lvalue reference to function,
\item pointer to member,
\item \tcode{std::nullptr_t}, or
\item a type that contains a placeholder type\iref{dcl.spec.auto}.
\end{itemize}
\pnum
\begin{note}
Other types are disallowed either explicitly below or implicitly by
the rules governing the form of
\grammarterm{template-argument}{s}\iref{temp.arg}.
\end{note}
The top-level
\grammarterm{cv-qualifier}{s}
on the
\grammarterm{template-parameter}
are ignored when determining its type.
\pnum
A non-type non-reference
\grammarterm{template-parameter}
is a prvalue.
It shall not be assigned to or in any other way have its value changed.
A non-type non-reference
\grammarterm{template-parameter}
cannot have its address taken.
When a non-type non-reference
\grammarterm{template-parameter}
is used as an initializer for a reference, a temporary is always used.
\begin{example}
\begin{codeblock}
template<const X& x, int i> void f() {
i++; // error: change of template-parameter value
&x; // OK
&i; // error: address of non-reference template-parameter
int& ri = i; // error: non-const reference bound to temporary
const int& cri = i; // OK: const reference bound to temporary
}
\end{codeblock}
\end{example}
\pnum
A non-type
\grammarterm{template-parameter}
shall not be declared to have floating-point, class, or void type.
\begin{example}
\begin{codeblock}
template<double d> class X; // error
template<double* pd> class Y; // OK
template<double& rd> class Z; // OK
\end{codeblock}
\end{example}
\pnum
A non-type
\grammarterm{template-parameter}
\indextext{array!template parameter of type}%
of type ``array of \tcode{T}'' or
\indextext{function!template parameter of type}%
of function type \tcode{T}
is adjusted to be of type ``pointer to \tcode{T}''.
\begin{example}
\begin{codeblock}
template<int* a> struct R { @\commentellip@ };
template<int b[5]> struct S { @\commentellip@ };
int p;
R<&p> w; // OK
S<&p> x; // OK due to parameter adjustment
int v[5];
R<v> y; // OK due to implicit argument conversion
S<v> z; // OK due to both adjustment and conversion
\end{codeblock}
\end{example}
\pnum
A \grammarterm{partial-concept-id} is
a \grammarterm{concept-name} followed by
a sequence of \grammarterm{template-argument}{s}.
These template arguments
are used to form a \grammarterm{constraint-expression}
as described below.
\pnum
A \grammarterm{constrained-parameter} declares
a template parameter
whose kind (type, non-type, template) and type
match that of the prototype parameter\iref{temp.concept}
of the concept designated by
the \grammarterm{qualified-concept-name} in
the \grammarterm{constrained-parameter}.
Let \tcode{X} be the prototype parameter of the designated concept.
The declared template parameter is determined by
the kind of \tcode{X} (type, non-type, template)
and the optional ellipsis in the \grammarterm{constrained-parameter}
as follows.
\begin{itemize}
\item If \tcode{X} is a type \grammarterm{template-parameter},
the declared parameter is a type \grammarterm{template-parameter}.
\item If \tcode{X} is a non-type \grammarterm{template-parameter},
the declared parameter is a non-type \grammarterm{template-parameter}
having the same type as \tcode{X}.
\item If \tcode{X} is a template \grammarterm{template-parameter},
the declared parameter is a template \grammarterm{template-parameter}
having the same \grammarterm{template-parameter-list} as \tcode{X},
excluding default template arguments.
\item If the \grammarterm{qualified-concept-name} is followed by an ellipsis,
then the declared parameter is a template parameter pack\iref{temp.variadic}.
\end{itemize}
\begin{example}
\begin{codeblock}
template<typename T> concept C1 = true;
template<template<typename> class X> concept C2 = true;
template<int N> concept C3 = true;
template<typename... Ts> concept C4 = true;
template<char... Cs> concept C5 = true;
template<C1 T> void f1(); // OK, \tcode{T} is a type \grammarterm{template-parameter}
template<C2 X> void f2(); // OK, \tcode{X} is a template with one \grammarterm{type-parameter}
template<C3 N> void f3(); // OK, \tcode{N} has type \tcode{int}
template<C4... Ts> void f4(); // OK, \tcode{Ts} is a template parameter pack of types
template<C4 T> void f5(); // OK, \tcode{T} is a type \grammarterm{template-parameter}
template<C5... Cs> void f6(); // OK, \tcode{Cs} is a template parameter pack of \tcode{char}s
\end{codeblock}
\end{example}
\pnum
A \grammarterm{constrained-parameter} introduces
a \grammarterm{constraint-expression}\iref{temp.constr.decl}.
The expression is derived from
the \grammarterm{qualified-concept-name} \tcode{Q} in
the \grammarterm{constrained-parameter},
its designated concept \tcode{C}, and
the declared template parameter \tcode{P}.
\begin{itemize}
\item First, a template argument \tcode{A} is formed from \tcode{P}.
If \tcode{P} declares a template parameter pack\iref{temp.variadic}
and \tcode{C} is a variadic concept\iref{temp.concept},
then \tcode{A} is the pack expansion \tcode{P...}.
Otherwise,
\tcode{A} is the \grammarterm{id-expression} \tcode{P}.
% FIXME: This does not guarantee that the expression has the same
% namespace qualification as Q.
\item Then, an \grammarterm{id-expression} \tcode{E} is formed as follows.
If \tcode{Q} is a \grammarterm{concept-name},
then \tcode{E} is \tcode{C<A>}.
Otherwise,
\tcode{Q} is a \grammarterm{partial-concept-id}
of the form \tcode{C<A$_1$, A$_2$, ..., A$_n$>},
and \tcode{E} is \tcode{C<A, A$_1$, A$_2$, ..., A$_n$>}.
\item Finally, if \tcode{P} declares a template parameter pack
and \tcode{C} is not a variadic concept,
\tcode{E} is adjusted to be the \grammarterm{fold-expression}
\tcode{(E \&\& ...)}\iref{expr.prim.fold}.
\end{itemize}
\tcode{E} is the introduced \grammarterm{constraint-expression}.
\begin{example}
\begin{codeblock}
template<typename T> concept C1 = true;
template<typename... Ts> concept C2 = true;
template<typename T, typename U> concept C3 = true;
template<C1 T> struct s1; // associates \tcode{C1<T>}
template<C1... T> struct s2; // associates \tcode{(C1<T> \&\& ...)}
template<C2... T> struct s3; // associates \tcode{C2<T...>}
template<C3<int> T> struct s4; // associates \tcode{C3<T, int>}
\end{codeblock}
\end{example}
\pnum
A
\defnx{default template-argument}{\idxgram{template-argument}!default}
is a
\grammarterm{template-argument}\iref{temp.arg} specified after
\tcode{=}
in a
\grammarterm{template-parameter}.
A default
\grammarterm{template-argument}
may be specified for any kind of
\grammarterm{template-parameter}
(type, non-type, template)
that is not a template parameter pack\iref{temp.variadic}.
A default
\grammarterm{template-argument}
may be specified in a template declaration.
A default
\grammarterm{template-argument}
shall not be specified in the
\grammarterm{template-parameter-list}{s}
of the definition of a member of a class template that appears outside
of the member's class.
A default
\grammarterm{template-argument}
shall not be specified in a friend class template declaration.
If a friend function template declaration
specifies a default
\grammarterm{template-argument},
that declaration shall be a definition and shall be the only declaration of
the function template in the translation unit.
\pnum
The default \grammarterm{template-argument}
of a \grammarterm{constrained-parameter}
shall match the kind (type, non-type, template)
of the declared template parameter.
\begin{example}
\begin{codeblock}
template<typename T> concept C1 = true;
template<int N> concept C2 = true;
template<template<typename> class X> concept C3 = true;
template<typename T> struct S0;
template<C1 T = int> struct S1; // OK
template<C2 N = 0> struct S2; // OK
template<C3 X = S0> struct S3; // OK
template<C1 T = 0> struct S4; // error: default argument is not a type
\end{codeblock}
\end{example}
\pnum
The set of default
\grammarterm{template-argument}{s}
available for use is obtained by merging the default arguments
from all prior declarations of the template in the
same way default function arguments are\iref{dcl.fct.default}.
\begin{example}
\begin{codeblock}
template<class T1, class T2 = int> class A;
template<class T1 = int, class T2> class A;
\end{codeblock}
is equivalent to
\begin{codeblock}
template<class T1 = int, class T2 = int> class A;
\end{codeblock}
\end{example}
\pnum
If a
\grammarterm{template-parameter}
of a class template, variable template, or alias template has a default
\grammarterm{template-argument},
each subsequent
\grammarterm{template-parameter}
shall either have a default
\grammarterm{template-argument}
supplied
or be a template parameter pack. If a \grammarterm{template-parameter}
of a primary class template, primary variable template, or alias template
is a template parameter pack, it shall be the last
\grammarterm{template-parameter}.
A template parameter pack of a function template shall not be followed by
another
template parameter unless that template parameter can be deduced from the
parameter-type-list\iref{dcl.fct} of the function template or has a
default argument\iref{temp.deduct}.
A template parameter of a deduction guide template\iref{temp.deduct.guide}
that does not have a default argument
shall be deducible
from the parameter-type-list
of the deduction guide template.
\begin{example}
\begin{codeblock}
template<class T1 = int, class T2> class B; // error
// \tcode{U} can be neither deduced from the parameter-type-list nor specified
template<class... T, class... U> void f() { } // error
template<class... T, class U> void g() { } // error
\end{codeblock}
\end{example}
\pnum
A
\grammarterm{template-parameter}
shall
not be given default arguments by two different declarations in the same scope.
\begin{example}
\begin{codeblock}
template<class T = int> class X;
template<class T = int> class X { @\commentellip@ }; // error
\end{codeblock}
\end{example}
\indextext{\idxcode{<}!template and}%
\pnum
When parsing a
default
\grammarterm{template-argument}
for a non-type
\grammarterm{template-parameter},
the first non-nested
\tcode{>}
is taken as the end of the
\grammarterm{template-parameter-list}
rather than a greater-than operator.
\begin{example}
\begin{codeblock}
template<int i = 3 > 4 > // syntax error
class X { @\commentellip@ };
template<int i = (3 > 4) > // OK
class Y { @\commentellip@ };
\end{codeblock}
\end{example}
\pnum
A
\grammarterm{template-parameter}
of a template
\grammarterm{template-parameter}
is permitted to have a default
\grammarterm{template-argument}.
When such default arguments are specified, they apply to the template
\grammarterm{template-parameter}
in the scope of the template
\grammarterm{template-parameter}.
\begin{example}
\begin{codeblock}
template <class T = float> struct B {};
template <template <class TT = float> class T> struct A {
inline void f();
inline void g();
};
template <template <class TT> class T> void A<T>::f() {
T<> t; // error: \tcode{TT} has no default template argument
}
template <template <class TT = char> class T> void A<T>::g() {
T<> t; // OK, \tcode{T<char>}
}
\end{codeblock}
\end{example}
\pnum
If a \grammarterm{template-parameter} is a
\grammarterm{type-parameter} with an ellipsis prior to its
optional \grammarterm{identifier} or is a
\grammarterm{parameter-declaration} that declares a
pack\iref{dcl.fct}, then the \grammarterm{template-parameter}
is a template parameter pack\iref{temp.variadic}.
A template parameter pack that is a \grammarterm{parameter-declaration} whose type
contains one or more unexpanded packs is a pack expansion. Similarly,
a template parameter pack that is a \grammarterm{type-parameter} with a
\grammarterm{template-parameter-list} containing one or more unexpanded
packs is a pack expansion. A template parameter pack that is a pack
expansion shall not expand a template parameter pack declared in the same
\grammarterm{template-parameter-list}.
\begin{example}
\begin{codeblock}
template <class... Types> // \tcode{Types} is a template type parameter pack
class Tuple; // but not a pack expansion
template <class T, int... Dims> // \tcode{Dims} is a non-type template parameter pack
struct multi_array; // but not a pack expansion
template <class... T>
struct value_holder {
template <T... Values> struct apply { }; // \tcode{Values} is a non-type template parameter pack
}; // and a pack expansion
template <class... T, T... Values> // error: \tcode{Values} expands template type parameter
struct static_array; // pack \tcode{T} within the same template parameter list
\end{codeblock}
\end{example}
\rSec1[temp.names]{Names of template specializations}
\pnum
A template specialization\iref{temp.spec} can be referred to by a
\grammarterm{template-id}:
\begin{bnf}
\nontermdef{simple-template-id}\br
template-name \terminal{<} \opt{template-argument-list} \terminal{>}
\end{bnf}
\begin{bnf}
\nontermdef{template-id}\br
simple-template-id\br
operator-function-id \terminal{<} \opt{template-argument-list} \terminal{>}\br
literal-operator-id \terminal{<} \opt{template-argument-list} \terminal{>}
\end{bnf}
\begin{bnf}
\nontermdef{template-name}\br
identifier
\end{bnf}
\begin{bnf}
\nontermdef{template-argument-list}\br
template-argument \opt{\terminal{...}}\br
template-argument-list \terminal{,} template-argument \opt{\terminal{...}}
\end{bnf}
\begin{bnf}
\nontermdef{template-argument}\br
constant-expression\br
type-id\br
id-expression
\end{bnf}
\begin{note}
The name lookup rules\iref{basic.lookup} are used to associate the use of
a name with a template declaration;
that is, to identify a name as a
\grammarterm{template-name}.
\end{note}
\pnum
For a
\grammarterm{template-name}
to be explicitly qualified by the template arguments,
the name must be considered to refer to a template.
\begin{note}
Whether a name actually refers to a template
cannot be known in some cases
until after argument dependent lookup is done\iref{basic.lookup.argdep}.
\end{note}
A name is considered to refer to a template if
name lookup finds
a \grammarterm{template-name}
or an overload set that contains a function template.
A name is also considered to refer to a template if
it is an \grammarterm{unqualified-id}
followed by a \tcode{<}
and name lookup finds either one or more functions or finds nothing.
\pnum
\indextext{\idxcode{<}!template and}%
When a name is considered to be a
\grammarterm{template-name},
and it is followed by a \tcode{<},
the \tcode{<}
is always taken as the delimiter of a
\grammarterm{template-argument-list}
and never as the less-than operator.
When parsing a \grammarterm{template-argument-list},
the first non-nested
\tcode{>}\footnote{A \tcode{>} that encloses the \grammarterm{type-id}
of a \tcode{dynamic_cast}, \tcode{static_cast}, \tcode{reinterpret_cast}
or \tcode{const_cast}, or which encloses the \grammarterm{template-argument}{s}
of a subsequent \grammarterm{template-id}, is considered nested for the purpose
of this description.
}
is taken as the ending delimiter
rather than a greater-than operator.
Similarly, the first non-nested \tcode{>{>}} is treated as two
consecutive but distinct \tcode{>} tokens, the first of which is taken
as the end of the \grammarterm{template-argument-list} and completes
the \grammarterm{template-id}. \begin{note} The second \tcode{>}
token produced by this replacement rule may terminate an enclosing
\grammarterm{template-id} construct or it may be part of a different
construct (e.g., a cast).\end{note}
\begin{example}
\begin{codeblock}
template<int i> class X { @\commentellip@ };
X< 1>2 > x1; // syntax error
X<(1>2)> x2; // OK
template<class T> class Y { @\commentellip@ };
Y<X<1>> x3; // OK, same as \tcode{Y<X<1> > x3;}
Y<X<6>>1>> x4; // syntax error
Y<X<(6>>1)>> x5; // OK
\end{codeblock}
\end{example}
\pnum
The keyword \tcode{template} is said to appear at the top level in
a \grammarterm{qualified-id}
if it appears outside of a \grammarterm{template-argument-list} or
\grammarterm{decltype-specifier}.
In a \grammarterm{qualified-id} of a \grammarterm{declarator-id} or
in a \grammarterm{qualified-id} formed by a \grammarterm{class-head-name}\iref{class} or
\grammarterm{enum-head-name}\iref{dcl.enum},
the keyword \tcode{template} shall not appear at the top level.
In a \grammarterm{qualified-id} used as the name in a
\grammarterm{typename-specifier}\iref{temp.res},
\grammarterm{elaborated-type-specifier}\iref{dcl.type.elab},
\grammarterm{using-declaration}\iref{namespace.udecl}, or
\grammarterm{class-or-decltype}\iref{class.derived},
an optional keyword \tcode{template} appearing at the top level is ignored.
In these contexts, a \tcode{<} token is always assumed to introduce a
\grammarterm{template-argument-list}.
In all other contexts, when naming a template specialization of
a member of an unknown specialization\iref{temp.dep.type},
the member template name shall be prefixed by the keyword \tcode{template}.
\begin{example}
\begin{codeblock}
struct X {
template<std::size_t> X* alloc();
template<std::size_t> static X* adjust();
};
template<class T> void f(T* p) {
T* p1 = p->alloc<200>(); // ill-formed: \tcode{<} means less than
T* p2 = p->template alloc<200>(); // OK: \tcode{<} starts template argument list
T::adjust<100>(); // ill-formed: \tcode{<} means less than
T::template adjust<100>(); // OK: \tcode{<} starts template argument list
}
\end{codeblock}
\end{example}
\pnum
A name prefixed by the keyword
\tcode{template}
shall be a \grammarterm{template-id} or
the name shall refer to a class template or an alias template.
\begin{note}
The keyword
\tcode{template}
may not be applied to non-template members of class templates.
\end{note}
\begin{note}
As is the case with the
\tcode{typename}
prefix, the
\tcode{template}
prefix is allowed in cases where it is not strictly
necessary; i.e., when the \grammarterm{nested-name-specifier} or
the expression on the left of
the
\tcode{->}
or
\tcode{.}
is not dependent on a
\grammarterm{template-parameter}, or the use does not appear in the
scope of a template.
\end{note}
\begin{example}
\begin{codeblock}
template <class T> struct A {
void f(int);
template <class U> void f(U);
};
template <class T> void f(T t) {
A<T> a;
a.template f<>(t); // OK: calls template
a.template f(t); // error: not a \grammarterm{template-id}
}
template <class T> struct B {
template <class T2> struct C { };
};
// OK: \tcode{T::template C} names a class template:
template <class T, template <class X> class TT = T::template C> struct D { };
D<B<int> > db;
\end{codeblock}
\end{example}
\pnum
\indextext{specialization!class template}%
A
\grammarterm{simple-template-id}
that names a class template specialization is a
\grammarterm{class-name}\iref{class}.
\pnum
A \grammarterm{template-id} that names an alias template
specialization is a \grammarterm{type-name}.
\pnum
When the \grammarterm{template-name}
of a \grammarterm{simple-template-id}
names a constrained non-function template
or
a constrained template \grammarterm{template-parameter},
%%% FIXME: Do we need to say this?
but not a member template
that is a member of an unknown specialization\iref{temp.res},
and
all \grammarterm{template-argument}{s}
in the \grammarterm{simple-template-id}
are non-dependent\iref{temp.dep.temp},
the associated constraints\iref{temp.constr.decl}
of the constrained template
shall be satisfied\iref{temp.constr.constr}.
\begin{example}
\begin{codeblock}
template<typename T> concept C1 = sizeof(T) != sizeof(int);
template<C1 T> struct S1 { };
template<C1 T> using Ptr = T*;
S1<int>* p; // error: constraints not satisfied
Ptr<int> p; // error: constraints not satisfied
template<typename T>
struct S2 { Ptr<int> x; }; // error, no diagnostic required
template<typename T>
struct S3 { Ptr<T> x; }; // OK, satisfaction is not required
S3<int> x; // error: constraints not satisfied
template<template<C1 T> class X>
struct S4 {