Skip to content

Commit faea290

Browse files
authored
[spec] Improve variable template examples (#3713)
Make runnable.
1 parent 77e55ae commit faea290

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed

spec/template.dd

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1481,30 +1481,36 @@ $(GNAME ConstructorTemplate):
14811481

14821482
$(H2 $(LNAME2 variable-template, Enum & Variable Templates))
14831483

1484-
$(P Like aggregates and functions, manifest constant and variable
1485-
declarations can have template parameters, providing there is
1484+
$(P Like aggregates and functions, $(DDSUBLINK spec/declaration, variable-declarations, variable
1485+
declarations) and manifest constants can have template parameters, providing there is
14861486
an $(GLINK2 declaration, Initializer):)
14871487

1488+
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
14881489
------
1489-
enum string constant(TL...) = TL.stringof;
1490+
enum bool within(alias v, T) = v <= T.max && v >= T.min;
14901491
ubyte[T.sizeof] storage(T) = 0;
1491-
auto array(alias a) = a;
1492+
const triplet(alias v) = [v, v+1, v+2];
1493+
1494+
static assert(within!(-128F, byte));
1495+
static assert(storage!(int[2]).length == 8);
1496+
static assert(triplet!3 == [3, 4, 5]);
14921497
------
1498+
)
14931499

1494-
$(P These declarations are transformed into templates:)
1500+
$(P Those declarations are transformed into these *TemplateDeclaration*s:)
14951501

14961502
------
1497-
template constant(TL...)
1503+
template within(alias v, T)
14981504
{
1499-
enum string constant = TL.stringof;
1505+
enum bool within = v <= T.max && v >= T.min;
15001506
}
15011507
template storage(T)
15021508
{
15031509
ubyte[T.sizeof] storage = 0;
15041510
}
1505-
template array(alias a)
1511+
template triplet(alias v)
15061512
{
1507-
auto array = a;
1513+
const triplet = [v, v+1, v+2];
15081514
}
15091515
------
15101516

0 commit comments

Comments
 (0)