@@ -990,7 +990,8 @@ $(GNAME UnionTemplateDeclaration):
990
990
)
991
991
992
992
$(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):)
994
995
995
996
------
996
997
template $(CODE_HIGHLIGHT Bar)(T)
@@ -1012,6 +1013,9 @@ $(GNAME UnionTemplateDeclaration):
1012
1013
}
1013
1014
------
1014
1015
1016
+ $(P See also: $(RELATIVE_LINK2 template_this_parameter, Template This Parameters).
1017
+ )
1018
+
1015
1019
$(P Analogously to class templates, struct, union and interfaces
1016
1020
can be transformed into templates by supplying a template parameter list.
1017
1021
)
@@ -1170,10 +1174,11 @@ $(H2 $(LNAME2 function-templates, Function Templates))
1170
1174
---
1171
1175
)
1172
1176
1173
- $(H2 $(LNAME2 variable-template, Variable Templates))
1177
+ $(H2 $(LNAME2 variable-template, Enum & Variable Templates))
1174
1178
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):)
1177
1182
1178
1183
------
1179
1184
enum string constant(TL...) = TL.stringof;
@@ -1471,22 +1476,23 @@ $(H2 $(LNAME2 recursive_templates, Recursive Templates))
1471
1476
effects, such as compile time evaluation of non-trivial functions.
1472
1477
For example, a factorial template can be written:)
1473
1478
1479
+ $(SPEC_RUNNABLE_EXAMPLE_COMPILE
1474
1480
------
1475
- template factorial(int n : 1)
1476
- {
1477
- enum { factorial = 1 }
1478
- }
1479
-
1480
1481
template factorial(int n)
1481
1482
{
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);
1483
1487
}
1484
1488
1485
- void test()
1486
- {
1487
- writefln("%s", factorial!(4)); // prints 24
1488
- }
1489
+ static assert(factorial!(4) == 24);
1489
1490
------
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
+ )
1490
1496
1491
1497
$(H2 $(LNAME2 template_constraints, Template Constraints))
1492
1498
@@ -1519,7 +1525,7 @@ $(GNAME Constraint):
1519
1525
---
1520
1526
1521
1527
$(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) :)
1523
1529
1524
1530
---
1525
1531
import std.traits;
0 commit comments