Skip to content

Commit bc81175

Browse files
committed
[spec/template.dd] Add see also links & tweaks
Add various useful links. Mention enum templates in variable template section. Use `static if` for factorial template.
1 parent 1ca5e4d commit bc81175

File tree

1 file changed

+21
-15
lines changed

1 file changed

+21
-15
lines changed

spec/template.dd

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -990,7 +990,8 @@ $(GNAME UnionTemplateDeclaration):
990990
)
991991

992992
$(P If a template declares exactly one member, and that member is a class
993-
with the same name as the template:)
993+
with the same name as the template (see
994+
$(RELATIVE_LINK2 implicit_template_properties, Eponymous Templates):)
994995

995996
------
996997
template $(CODE_HIGHLIGHT Bar)(T)
@@ -1012,6 +1013,9 @@ $(GNAME UnionTemplateDeclaration):
10121013
}
10131014
------
10141015

1016+
$(P See also: $(RELATIVE_LINK2 template_this_parameter, Template This Parameters).
1017+
)
1018+
10151019
$(P Analogously to class templates, struct, union and interfaces
10161020
can be transformed into templates by supplying a template parameter list.
10171021
)
@@ -1170,10 +1174,11 @@ $(H2 $(LNAME2 function-templates, Function Templates))
11701174
---
11711175
)
11721176

1173-
$(H2 $(LNAME2 variable-template, Variable Templates))
1177+
$(H2 $(LNAME2 variable-template, Enum & Variable Templates))
11741178

1175-
$(P Same as aggregates and functions, variable declarations with
1176-
$(GLINK2 declaration, Initializer) can have optional template parameters:)
1179+
$(P Like aggregates and functions, manifest constant and variable
1180+
declarations can have template parameters, providing there is
1181+
an $(GLINK2 declaration, Initializer):)
11771182

11781183
------
11791184
enum string constant(TL...) = TL.stringof;
@@ -1471,22 +1476,23 @@ $(H2 $(LNAME2 recursive_templates, Recursive Templates))
14711476
effects, such as compile time evaluation of non-trivial functions.
14721477
For example, a factorial template can be written:)
14731478

1479+
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
14741480
------
1475-
template factorial(int n : 1)
1476-
{
1477-
enum { factorial = 1 }
1478-
}
1479-
14801481
template factorial(int n)
14811482
{
1482-
enum { factorial = n* factorial!(n-1) }
1483+
static if (n == 1)
1484+
enum factorial = 1;
1485+
else
1486+
enum factorial = n * factorial!(n - 1);
14831487
}
14841488

1485-
void test()
1486-
{
1487-
writefln("%s", factorial!(4)); // prints 24
1488-
}
1489+
static assert(factorial!(4) == 24);
14891490
------
1491+
)
1492+
$(P For more information and a $(ACRONYM CTFE, Compile-time Function Execution)
1493+
factorial alternative, see:
1494+
$(DDSUBLINK articles/templates-revisited, template-recursion, Template Recursion).
1495+
)
14901496

14911497
$(H2 $(LNAME2 template_constraints, Template Constraints))
14921498

@@ -1519,7 +1525,7 @@ $(GNAME Constraint):
15191525
---
15201526

15211527
$(P Template constraints can be used with aggregate types (structs, classes, unions).
1522-
Constraints are effectively used with library module "std.traits":)
1528+
Constraints are effectively used with library module $(MREF std, traits):)
15231529

15241530
---
15251531
import std.traits;

0 commit comments

Comments
 (0)