Skip to content

Commit 500a6eb

Browse files
authored
Merge pull request #3183 from ntrel/template-params
[spec/template.dd] Add *Template Parameters* heading Signed-off-by: Dennis <dkorpel@users.noreply.github.com> Merged-on-behalf-of: Dennis <dkorpel@users.noreply.github.com>
2 parents d30930b + 4a8431e commit 500a6eb

File tree

1 file changed

+67
-40
lines changed

1 file changed

+67
-40
lines changed

spec/template.dd

Lines changed: 67 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,6 @@ $(GNAME TemplateParameterList):
2121
$(GLINK TemplateParameter)
2222
$(GLINK TemplateParameter) $(D ,)
2323
$(GLINK TemplateParameter) $(D ,) $(GSELF TemplateParameterList)
24-
25-
$(GNAME TemplateParameter):
26-
$(GLINK TemplateTypeParameter)
27-
$(GLINK TemplateValueParameter)
28-
$(GLINK TemplateAliasParameter)
29-
$(GLINK TemplateSequenceParameter)
30-
$(GLINK TemplateThisParameter)
3124
)
3225

3326
$(P The *DeclDefs* body of the template must be syntactically correct
@@ -37,8 +30,8 @@ $(GNAME TemplateParameter):
3730
enums, variables, functions, and other templates.
3831
)
3932

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.)
4235

4336
---
4437
template t(T) // declare type parameter T
@@ -47,19 +40,16 @@ $(GNAME TemplateParameter):
4740
}
4841
---
4942

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.)
5546

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+
---
6353

6454
$(P If multiple templates with the same $(I Identifier) are
6555
declared, they are distinct if they have different parameters
@@ -245,10 +235,37 @@ $(H2 $(LNAME2 instantiation_scope, Instantiation Scope))
245235
------
246236

247237
$(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).
249239
)
250240

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))
252269

253270
$(P The types of template parameters are deduced for a particular
254271
template instantiation by comparing the template argument with
@@ -322,7 +339,7 @@ $(H2 $(LNAME2 argument_deduction, Argument Deduction))
322339
// (3) U is B
323340
------
324341

325-
$(H2 $(LNAME2 template_type_parameters, Template Type Parameters))
342+
$(H3 $(LNAME2 template_type_parameters, Type Parameters))
326343

327344
$(GRAMMAR
328345
$(GNAME TemplateTypeParameter):
@@ -338,7 +355,7 @@ $(GNAME TemplateTypeParameterDefault):
338355
$(D =) $(GLINK2 type, Type)
339356
)
340357

341-
$(H3 $(LNAME2 parameters_specialization, Specialization))
358+
$(H4 $(LNAME2 parameters_specialization, Specialization))
342359

343360
$(P Templates may be specialized for particular types of arguments
344361
by following the template parameter identifier with a : and the
@@ -364,7 +381,7 @@ $(H3 $(LNAME2 parameters_specialization, Specialization))
364381
)
365382

366383

367-
$(H2 $(LNAME2 template_this_parameter, Template This Parameters))
384+
$(H3 $(LNAME2 template_this_parameter, This Parameters))
368385

369386
$(GRAMMAR
370387
$(GNAME TemplateThisParameter):
@@ -406,7 +423,9 @@ S
406423
immutable(S)
407424
)
408425

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,
410429
consider the implementation of a final base method which returns a derived
411430
class type. Typically this would return a base type, but that would prohibit
412431
calling or accessing derived properties of the type:)
@@ -436,7 +455,7 @@ immutable(S)
436455
---
437456

438457
$(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:)
440459

441460
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
442461
---
@@ -469,7 +488,7 @@ immutable(S)
469488
---
470489
)
471490

472-
$(H2 $(LNAME2 template_value_parameter, Template Value Parameters))
491+
$(H3 $(LNAME2 template_value_parameter, Value Parameters))
473492

474493
$(GRAMMAR
475494
$(GNAME TemplateValueParameter):
@@ -508,12 +527,15 @@ $(GNAME TemplateValueParameterDefault):
508527
-----
509528
)
510529

530+
$(H4 $(LNAME2 value_specialization, Specialization))
531+
511532
$(P Any specialization or default expression provided must be evaluatable
512533
at compile-time.)
513534

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`:)
516537

538+
$(SPEC_RUNNABLE_EXAMPLE_RUN
517539
------
518540
template foo(U : int, int v : 10)
519541
{
@@ -523,10 +545,15 @@ $(GNAME TemplateValueParameterDefault):
523545
void main()
524546
{
525547
assert(foo!(int, 10).x == 10);
548+
static assert(!__traits(compiles, foo!(int, 11)));
526549
}
527550
------
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.)
528555

529-
$(H2 $(LNAME2 aliasparameters, Template Alias Parameters))
556+
$(H3 $(LNAME2 aliasparameters, Alias Parameters))
530557

531558
$(GRAMMAR
532559
$(GNAME TemplateAliasParameter):
@@ -740,7 +767,7 @@ $(GNAME TemplateAliasParameterDefault):
740767
))
741768
)
742769

743-
$(H3 $(LNAME2 typed_alias_op, Typed alias parameters))
770+
$(H4 $(LNAME2 typed_alias_op, Typed Alias Parameters))
744771

745772
$(P Alias parameters can also be typed.
746773
These parameters will accept symbols of that type:)
@@ -754,7 +781,7 @@ $(H3 $(LNAME2 typed_alias_op, Typed alias parameters))
754781
Foo!f; // fails to instantiate
755782
------
756783

757-
$(H3 $(LNAME2 alias_parameter_specialization, Specialization))
784+
$(H4 $(LNAME2 alias_parameter_specialization, Specialization))
758785

759786
$(P Alias parameters can accept both literals and user-defined type symbols,
760787
but they are less specialized than the matches to type parameters and
@@ -782,7 +809,7 @@ $(H3 $(LNAME2 alias_parameter_specialization, Specialization))
782809
alias bar = Bar!(C!int); // instantiates #5
783810
------
784811

785-
$(H2 $(LNAME2 variadic-templates, Template Sequence Parameters))
812+
$(H3 $(LNAME2 variadic-templates, Sequence Parameters))
786813

787814
$(GRAMMAR
788815
$(GNAME TemplateSequenceParameter):
@@ -846,7 +873,7 @@ $(GNAME TemplateSequenceParameter):
846873
to dynamically change, add, or remove elements either at compile-time or run-time.
847874
)
848875

849-
$(H3 $(LNAME2 typeseq_deduction, Type Sequence Deduction))
876+
$(H4 $(LNAME2 typeseq_deduction, Type Sequence Deduction))
850877

851878
$(P Type sequences can be deduced from the trailing parameters
852879
of an $(RELATIVE_LINK2 ifti, implicitly instantiated) function template:)
@@ -913,7 +940,7 @@ a
913940
)
914941
See also: $(REF partial, std,functional)
915942

916-
$(H3 $(LNAME2 variadic_template_specialization, Specialization))
943+
$(H4 $(LNAME2 variadic_template_specialization, Specialization))
917944

918945
$(P If both a template with a sequence parameter and a template
919946
without a sequence parameter exactly match a template instantiation,
@@ -935,9 +962,9 @@ $(H3 $(LNAME2 variadic_template_specialization, Specialization))
935962
alias foo4 = Foo!(int, 3, std); // instantiates #4
936963
----
937964

938-
$(H2 $(LNAME2 template_parameter_def_values, Template Parameter Default Values))
965+
$(H3 $(LNAME2 template_parameter_def_values, Default Arguments))
939966

940-
$(P Trailing template parameters can be given default values:)
967+
$(P Trailing template parameters can be given default arguments:)
941968

942969
------
943970
template Foo(T, U = int) { ... }
@@ -1032,7 +1059,7 @@ $(GNAME UnionTemplateDeclaration):
10321059
}
10331060
------
10341061

1035-
$(P See also: $(RELATIVE_LINK2 template_this_parameter, Template This Parameters).
1062+
$(P See also: $(RELATIVE_LINK2 template_this_parameter, This Parameters).
10361063
)
10371064

10381065
$(P Analogously to class templates, struct, union and interfaces

0 commit comments

Comments
 (0)