@@ -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;
@@ -1473,22 +1478,23 @@ $(H2 $(LNAME2 recursive_templates, Recursive Templates))
1473
1478
effects, such as compile time evaluation of non-trivial functions.
1474
1479
For example, a factorial template can be written:)
1475
1480
1481
+ $(SPEC_RUNNABLE_EXAMPLE_COMPILE
1476
1482
------
1477
- template factorial(int n : 1)
1478
- {
1479
- enum { factorial = 1 }
1480
- }
1481
-
1482
1483
template factorial(int n)
1483
1484
{
1484
- enum { factorial = n* factorial!(n-1) }
1485
+ static if (n == 1)
1486
+ enum factorial = 1;
1487
+ else
1488
+ enum factorial = n * factorial!(n - 1);
1485
1489
}
1486
1490
1487
- void test()
1488
- {
1489
- writefln("%s", factorial!(4)); // prints 24
1490
- }
1491
+ static assert(factorial!(4) == 24);
1491
1492
------
1493
+ )
1494
+ $(P For more information and a $(ACRONYM CTFE, Compile-time Function Execution)
1495
+ factorial alternative, see:
1496
+ $(DDSUBLINK articles/templates-revisited, template-recursion, Template Recursion).
1497
+ )
1492
1498
1493
1499
$(H2 $(LNAME2 template_constraints, Template Constraints))
1494
1500
@@ -1521,7 +1527,7 @@ $(GNAME Constraint):
1521
1527
---
1522
1528
1523
1529
$(P Template constraints can be used with aggregate types (structs, classes, unions).
1524
- Constraints are effectively used with library module " std. traits" :)
1530
+ Constraints are effectively used with library module $(MREF std, traits) :)
1525
1531
1526
1532
---
1527
1533
import std.traits;
0 commit comments