Skip to content

Commit 4e8ed45

Browse files
authored
[spec/template] Improve alias template docs (#3570)
* [spec/template] Improve alias template docs Also make 2 IFTI examples runnable. Document that IFTI doesn't work with a parameter type that is an alias template instance. Add ElementType alias example. * `is` can't match an alias template instance TypeSpecialization Add link to template parameter specialization.
1 parent a412cc8 commit 4e8ed45

File tree

2 files changed

+53
-7
lines changed

2 files changed

+53
-7
lines changed

spec/expression.dd

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3055,10 +3055,11 @@ $(D is $(LPAREN)) $(I Type) $(I Identifier) $(D ==) $(I TypeSpecialization) $(D
30553055
)
30563056

30573057
$(P
3058-
More complex types can be pattern matched; the
3058+
More complex types can be pattern matched. The
30593059
$(I TemplateParameterList) declares symbols based on the
30603060
parts of the pattern that are matched, analogously to the
3061-
way implied template parameters are matched.
3061+
way $(DDSUBLINK spec/template, parameters_specialization,
3062+
implied template parameters) are matched.
30623063
)
30633064

30643065
$(P $(B Example:) Matching a Template Instantiation))
@@ -3081,6 +3082,20 @@ $(SPEC_RUNNABLE_EXAMPLE_COMPILE
30813082
---
30823083
)
30833084

3085+
$(P *Type* cannot be matched when *TypeSpecialization* is an
3086+
$(DDSUBLINK spec/template, alias-template, alias template) instance:
3087+
)
3088+
3089+
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
3090+
---
3091+
struct S(T) {}
3092+
alias A(T) = S!T;
3093+
3094+
static assert(is(A!int : S!T, T));
3095+
static assert(!is(A!int : A!T, T));
3096+
---
3097+
)
3098+
30843099
$(P $(B Example:) Matching an Associative Array)
30853100

30863101
$(SPEC_RUNNABLE_EXAMPLE_COMPILE

spec/template.dd

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1250,19 +1250,25 @@ $(H4 $(LNAME2 ifti-restrictions, Restrictions))
12501250
$(P Function template type parameters that are to be implicitly
12511251
deduced must appear in the type of at least one function parameter:)
12521252

1253+
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
12531254
------
1254-
void foo(T : U*, U)(U t) { ... }
1255+
void foo(T : U*, U)(U t) {}
12551256

1256-
int x;
1257-
foo!(int*)(x); // ok, U is deduced and T is specified explicitly
1258-
foo(x); // error, only U can be deduced, not T
1257+
void main()
1258+
{
1259+
int x;
1260+
foo!(int*)(x); // ok, U is deduced and T is specified explicitly
1261+
//foo(x); // error, only U can be deduced, not T
1262+
}
12591263
------
1264+
)
12601265

12611266
$(P When the template parameters must be deduced, the
12621267
$(RELATIVE_LINK2 implicit_template_properties, eponymous members)
12631268
can't rely on a $(LINK2 version.html#StaticIfCondition, `static if`)
12641269
condition since the deduction relies on how the members are used:)
12651270

1271+
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
12661272
------
12671273
template foo(T)
12681274
{
@@ -1272,10 +1278,30 @@ $(H4 $(LNAME2 ifti-restrictions, Restrictions))
12721278

12731279
void main()
12741280
{
1275-
foo(0); // Error: cannot deduce function from argument types
1281+
//foo(0); // Error: cannot deduce function from argument types
12761282
foo!int(0); // Ok since no deduction necessary
12771283
}
12781284
------
1285+
)
1286+
1287+
$(P IFTI does not work when the parameter type is an
1288+
$(RELATIVE_LINK2 alias-template, alias template) instance:
1289+
)
1290+
1291+
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
1292+
---
1293+
struct S(T) {}
1294+
alias A(T) = S!T;
1295+
void f(T)(A!T) {}
1296+
1297+
void main()
1298+
{
1299+
A!int v;
1300+
//f(v); // error
1301+
f!int(v); // OK
1302+
}
1303+
---
1304+
)
12791305

12801306
$(H4 $(LNAME2 ifti-conversions, Type Conversions))
12811307

@@ -1489,12 +1515,17 @@ $(H2 $(LNAME2 alias-template, Alias Templates))
14891515
parameters:)
14901516

14911517
------
1518+
alias ElementType(T : T[]) = T;
14921519
alias Sequence(TL...) = TL;
14931520
------
14941521

14951522
It is lowered to:
14961523

14971524
------
1525+
template ElementType(T : T[])
1526+
{
1527+
alias ElementType = T;
1528+
}
14981529
template Sequence(TL...)
14991530
{
15001531
alias Sequence = TL;

0 commit comments

Comments
 (0)