Skip to content

Commit c54452f

Browse files
authored
Merge pull request #2826 from ntrel/template-tweak
[spec/template.dd] Add see also links & tweaks merged-on-behalf-of: Nicholas Wilson <thewilsonator@users.noreply.github.com>
2 parents 6ea5532 + e3fd38a commit c54452f

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;
@@ -1473,22 +1478,23 @@ $(H2 $(LNAME2 recursive_templates, Recursive Templates))
14731478
effects, such as compile time evaluation of non-trivial functions.
14741479
For example, a factorial template can be written:)
14751480

1481+
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
14761482
------
1477-
template factorial(int n : 1)
1478-
{
1479-
enum { factorial = 1 }
1480-
}
1481-
14821483
template factorial(int n)
14831484
{
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);
14851489
}
14861490

1487-
void test()
1488-
{
1489-
writefln("%s", factorial!(4)); // prints 24
1490-
}
1491+
static assert(factorial!(4) == 24);
14911492
------
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+
)
14921498

14931499
$(H2 $(LNAME2 template_constraints, Template Constraints))
14941500

@@ -1521,7 +1527,7 @@ $(GNAME Constraint):
15211527
---
15221528

15231529
$(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):)
15251531

15261532
---
15271533
import std.traits;

0 commit comments

Comments
 (0)