Skip to content

Commit be7bde0

Browse files
committed
[spec/template.dd] Add *Template Parameters* heading
TemplateDeclaration: Move template parameter grammar & info to parameter section. Add parameter specialization example. (Specialization needs to be defined here as it is mentioned for template overloading). Parameters: Use 'default arguments' not 'default values' as they can be types. Make various template parameter headings into subheadings. Rename *Argument Deduction* section to *Parameter Type Deduction* and make it a subheading too. (Later this should be moved to a subheading of *Type Parameters*). Add *Avoiding Runtime Type Checks* subheading for TemplateThisParameter. Add *Specialization* subheading for TemplateValueParameter and explain example, make runnable.
1 parent 75d105f commit be7bde0

File tree

1 file changed

+63
-36
lines changed

1 file changed

+63
-36
lines changed

spec/template.dd

Lines changed: 63 additions & 36 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, Template 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, Template 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:)
@@ -462,7 +481,7 @@ immutable(S)
462481
}
463482
---
464483

465-
$(H2 $(LNAME2 template_value_parameter, Template Value Parameters))
484+
$(H3 $(LNAME2 template_value_parameter, Template Value Parameters))
466485

467486
$(GRAMMAR
468487
$(GNAME TemplateValueParameter):
@@ -501,12 +520,15 @@ $(GNAME TemplateValueParameterDefault):
501520
-----
502521
)
503522

523+
$(H4 $(LNAME2 value_specialization, Specialization))
524+
504525
$(P Any specialization or default expression provided must be evaluatable
505526
at compile-time.)
506527

507-
$(P This example of template foo has a value parameter that
508-
is specialized for 10:)
528+
$(P In this example, template `foo` has a value parameter that
529+
is specialized for `10`:)
509530

531+
$(SPEC_RUNNABLE_EXAMPLE_RUN
510532
------
511533
template foo(U : int, int v : 10)
512534
{
@@ -516,10 +538,15 @@ $(GNAME TemplateValueParameterDefault):
516538
void main()
517539
{
518540
assert(foo!(int, 10).x == 10);
541+
static assert(!__traits(compiles, foo!(int, 11)));
519542
}
520543
------
544+
)
545+
546+
$(P This can be useful when a different template body is required for a specific value.
547+
Another template overload would be defined to take other integer literal values.)
521548

522-
$(H2 $(LNAME2 aliasparameters, Template Alias Parameters))
549+
$(H3 $(LNAME2 aliasparameters, Template Alias Parameters))
523550

524551
$(GRAMMAR
525552
$(GNAME TemplateAliasParameter):
@@ -733,7 +760,7 @@ $(GNAME TemplateAliasParameterDefault):
733760
))
734761
)
735762

736-
$(H3 $(LNAME2 typed_alias_op, Typed alias parameters))
763+
$(H4 $(LNAME2 typed_alias_op, Typed alias parameters))
737764

738765
$(P Alias parameters can also be typed.
739766
These parameters will accept symbols of that type:)
@@ -747,7 +774,7 @@ $(H3 $(LNAME2 typed_alias_op, Typed alias parameters))
747774
Foo!f; // fails to instantiate
748775
------
749776

750-
$(H3 $(LNAME2 alias_parameter_specialization, Specialization))
777+
$(H4 $(LNAME2 alias_parameter_specialization, Specialization))
751778

752779
$(P Alias parameters can accept both literals and user-defined type symbols,
753780
but they are less specialized than the matches to type parameters and
@@ -775,7 +802,7 @@ $(H3 $(LNAME2 alias_parameter_specialization, Specialization))
775802
alias bar = Bar!(C!int); // instantiates #5
776803
------
777804

778-
$(H2 $(LNAME2 variadic-templates, Template Sequence Parameters))
805+
$(H3 $(LNAME2 variadic-templates, Template Sequence Parameters))
779806

780807
$(GRAMMAR
781808
$(GNAME TemplateSequenceParameter):
@@ -928,9 +955,9 @@ $(H3 $(LNAME2 variadic_template_specialization, Specialization))
928955
alias foo4 = Foo!(int, 3, std); // instantiates #4
929956
----
930957

931-
$(H2 $(LNAME2 template_parameter_def_values, Template Parameter Default Values))
958+
$(H3 $(LNAME2 template_parameter_def_values, Template Parameter Default Arguments))
932959

933-
$(P Trailing template parameters can be given default values:)
960+
$(P Trailing template parameters can be given default arguments:)
934961

935962
------
936963
template Foo(T, U = int) { ... }

0 commit comments

Comments
 (0)