@@ -21,13 +21,6 @@ $(GNAME TemplateParameterList):
21
21
$(GLINK TemplateParameter)
22
22
$(GLINK TemplateParameter) $(D ,)
23
23
$(GLINK TemplateParameter) $(D ,) $(GSELF TemplateParameterList)
24
-
25
- $(GNAME TemplateParameter):
26
- $(GLINK TemplateTypeParameter)
27
- $(GLINK TemplateValueParameter)
28
- $(GLINK TemplateAliasParameter)
29
- $(GLINK TemplateSequenceParameter)
30
- $(GLINK TemplateThisParameter)
31
24
)
32
25
33
26
$(P The *DeclDefs* body of the template must be syntactically correct
@@ -37,8 +30,8 @@ $(GNAME TemplateParameter):
37
30
enums, variables, functions, and other templates.
38
31
)
39
32
40
- $(P Template parameters can take types, values, symbols, or sequences.
41
- Type parameters can take any type .)
33
+ $(P $(RELATIVE_LINK2 parameters, Template parameters) can take types,
34
+ values, symbols, or sequences .)
42
35
43
36
---
44
37
template t(T) // declare type parameter T
@@ -47,19 +40,16 @@ $(GNAME TemplateParameter):
47
40
}
48
41
---
49
42
50
- $(P Value parameters can take any expression which can be statically
51
- evaluated at compile time.
52
- Alias parameters can take almost any symbol.
53
- Sequence parameters can take zero or more types, values or symbols.
54
- )
43
+ $(P A template parameter can have a *specialization* which
44
+ constrains an argument the $(I TemplateParameter) can
45
+ accept.)
55
46
56
- $(P Template parameter specializations
57
- constrain the values or types the $(I TemplateParameter) can
58
- accept.
59
- )
60
- $(P Template parameter defaults are the value or type to use for the
61
- $(I TemplateParameter) in case one is not supplied.
62
- )
47
+ ---
48
+ template t(T : int) // type T must implicitly convert to int
49
+ {
50
+ ...
51
+ }
52
+ ---
63
53
64
54
$(P If multiple templates with the same $(I Identifier) are
65
55
declared, they are distinct if they have different parameters
@@ -245,10 +235,37 @@ $(H2 $(LNAME2 instantiation_scope, Instantiation Scope))
245
235
------
246
236
247
237
$(P $(I TemplateParameter) specializations and default
248
- values are evaluated in the scope of the $(I TemplateDeclaration).
238
+ arguments are evaluated in the scope of the $(I TemplateDeclaration).
249
239
)
250
240
251
- $(H2 $(LNAME2 argument_deduction, Argument Deduction))
241
+
242
+ $(H2 $(LNAME2 parameters, Template Parameters))
243
+
244
+ $(GRAMMAR
245
+ $(GNAME TemplateParameter):
246
+ $(GLINK TemplateTypeParameter)
247
+ $(GLINK TemplateValueParameter)
248
+ $(GLINK TemplateAliasParameter)
249
+ $(GLINK TemplateSequenceParameter)
250
+ $(GLINK TemplateThisParameter)
251
+ )
252
+
253
+ $(P Template parameters can take types, values, symbols, or sequences.)
254
+
255
+ $(UL
256
+ $(LI Type parameters can take any type.)
257
+ $(LI Value parameters can take any expression which can be statically
258
+ evaluated at compile time.)
259
+ $(LI Alias parameters can take almost any symbol.)
260
+ $(LI Sequence parameters can take zero or more types, values or symbols.)
261
+ )
262
+
263
+ $(P $(RELATIVE_LINK2 template_parameter_def_values, A default argument)
264
+ specifies the type, value or symbol to use for the
265
+ $(I TemplateParameter) when a matching argument is not supplied.
266
+ )
267
+
268
+ $(H3 $(LNAME2 argument_deduction, Type Parameter Deduction))
252
269
253
270
$(P The types of template parameters are deduced for a particular
254
271
template instantiation by comparing the template argument with
@@ -322,7 +339,7 @@ $(H2 $(LNAME2 argument_deduction, Argument Deduction))
322
339
// (3) U is B
323
340
------
324
341
325
- $(H2 $(LNAME2 template_type_parameters, Template Type Parameters))
342
+ $(H3 $(LNAME2 template_type_parameters, Type Parameters))
326
343
327
344
$(GRAMMAR
328
345
$(GNAME TemplateTypeParameter):
@@ -338,7 +355,7 @@ $(GNAME TemplateTypeParameterDefault):
338
355
$(D =) $(GLINK2 type, Type)
339
356
)
340
357
341
- $(H3 $(LNAME2 parameters_specialization, Specialization))
358
+ $(H4 $(LNAME2 parameters_specialization, Specialization))
342
359
343
360
$(P Templates may be specialized for particular types of arguments
344
361
by following the template parameter identifier with a : and the
@@ -364,7 +381,7 @@ $(H3 $(LNAME2 parameters_specialization, Specialization))
364
381
)
365
382
366
383
367
- $(H2 $(LNAME2 template_this_parameter, Template This Parameters))
384
+ $(H3 $(LNAME2 template_this_parameter, This Parameters))
368
385
369
386
$(GRAMMAR
370
387
$(GNAME TemplateThisParameter):
406
423
immutable(S)
407
424
)
408
425
409
- $(P This is especially useful when used with inheritance. For example,
426
+ $(H4 $(LNAME2 this_rtti, Avoiding Runtime Type Checks))
427
+
428
+ $(P *TemplateThisParameter* is especially useful when used with inheritance. For example,
410
429
consider the implementation of a final base method which returns a derived
411
430
class type. Typically this would return a base type, but that would prohibit
412
431
calling or accessing derived properties of the type:)
@@ -436,7 +455,7 @@ immutable(S)
436
455
---
437
456
438
457
$(P Here the method $(D add) returns the base type, which doesn't implement the
439
- $(D remove) method. The $(D template this) parameter can be used for this purpose:)
458
+ $(D remove) method. The template ` this` parameter can be used for this purpose:)
440
459
441
460
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
442
461
---
@@ -469,7 +488,7 @@ immutable(S)
469
488
---
470
489
)
471
490
472
- $(H2 $(LNAME2 template_value_parameter, Template Value Parameters))
491
+ $(H3 $(LNAME2 template_value_parameter, Value Parameters))
473
492
474
493
$(GRAMMAR
475
494
$(GNAME TemplateValueParameter):
@@ -508,12 +527,15 @@ $(GNAME TemplateValueParameterDefault):
508
527
-----
509
528
)
510
529
530
+ $(H4 $(LNAME2 value_specialization, Specialization))
531
+
511
532
$(P Any specialization or default expression provided must be evaluatable
512
533
at compile-time.)
513
534
514
- $(P This example of template foo has a value parameter that
515
- is specialized for 10 :)
535
+ $(P In this example, template ` foo` has a value parameter that
536
+ is specialized for `10` :)
516
537
538
+ $(SPEC_RUNNABLE_EXAMPLE_RUN
517
539
------
518
540
template foo(U : int, int v : 10)
519
541
{
@@ -523,10 +545,15 @@ $(GNAME TemplateValueParameterDefault):
523
545
void main()
524
546
{
525
547
assert(foo!(int, 10).x == 10);
548
+ static assert(!__traits(compiles, foo!(int, 11)));
526
549
}
527
550
------
551
+ )
552
+
553
+ $(P This can be useful when a different template body is required for a specific value.
554
+ Another template overload would be defined to take other integer literal values.)
528
555
529
- $(H2 $(LNAME2 aliasparameters, Template Alias Parameters))
556
+ $(H3 $(LNAME2 aliasparameters, Alias Parameters))
530
557
531
558
$(GRAMMAR
532
559
$(GNAME TemplateAliasParameter):
@@ -740,7 +767,7 @@ $(GNAME TemplateAliasParameterDefault):
740
767
))
741
768
)
742
769
743
- $(H3 $(LNAME2 typed_alias_op, Typed alias parameters ))
770
+ $(H4 $(LNAME2 typed_alias_op, Typed Alias Parameters ))
744
771
745
772
$(P Alias parameters can also be typed.
746
773
These parameters will accept symbols of that type:)
@@ -754,7 +781,7 @@ $(H3 $(LNAME2 typed_alias_op, Typed alias parameters))
754
781
Foo!f; // fails to instantiate
755
782
------
756
783
757
- $(H3 $(LNAME2 alias_parameter_specialization, Specialization))
784
+ $(H4 $(LNAME2 alias_parameter_specialization, Specialization))
758
785
759
786
$(P Alias parameters can accept both literals and user-defined type symbols,
760
787
but they are less specialized than the matches to type parameters and
@@ -782,7 +809,7 @@ $(H3 $(LNAME2 alias_parameter_specialization, Specialization))
782
809
alias bar = Bar!(C!int); // instantiates #5
783
810
------
784
811
785
- $(H2 $(LNAME2 variadic-templates, Template Sequence Parameters))
812
+ $(H3 $(LNAME2 variadic-templates, Sequence Parameters))
786
813
787
814
$(GRAMMAR
788
815
$(GNAME TemplateSequenceParameter):
@@ -846,7 +873,7 @@ $(GNAME TemplateSequenceParameter):
846
873
to dynamically change, add, or remove elements either at compile-time or run-time.
847
874
)
848
875
849
- $(H3 $(LNAME2 typeseq_deduction, Type Sequence Deduction))
876
+ $(H4 $(LNAME2 typeseq_deduction, Type Sequence Deduction))
850
877
851
878
$(P Type sequences can be deduced from the trailing parameters
852
879
of an $(RELATIVE_LINK2 ifti, implicitly instantiated) function template:)
913
940
)
914
941
See also: $(REF partial, std,functional)
915
942
916
- $(H3 $(LNAME2 variadic_template_specialization, Specialization))
943
+ $(H4 $(LNAME2 variadic_template_specialization, Specialization))
917
944
918
945
$(P If both a template with a sequence parameter and a template
919
946
without a sequence parameter exactly match a template instantiation,
@@ -935,9 +962,9 @@ $(H3 $(LNAME2 variadic_template_specialization, Specialization))
935
962
alias foo4 = Foo!(int, 3, std); // instantiates #4
936
963
----
937
964
938
- $(H2 $(LNAME2 template_parameter_def_values, Template Parameter Default Values ))
965
+ $(H3 $(LNAME2 template_parameter_def_values, Default Arguments ))
939
966
940
- $(P Trailing template parameters can be given default values :)
967
+ $(P Trailing template parameters can be given default arguments :)
941
968
942
969
------
943
970
template Foo(T, U = int) { ... }
@@ -1032,7 +1059,7 @@ $(GNAME UnionTemplateDeclaration):
1032
1059
}
1033
1060
------
1034
1061
1035
- $(P See also: $(RELATIVE_LINK2 template_this_parameter, Template This Parameters).
1062
+ $(P See also: $(RELATIVE_LINK2 template_this_parameter, This Parameters).
1036
1063
)
1037
1064
1038
1065
$(P Analogously to class templates, struct, union and interfaces
0 commit comments