/
template.dd
1456 lines (1164 loc) · 39.3 KB
/
template.dd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
Ddoc
$(SPEC_S Templates,
$(BLOCKQUOTE_BY Richard Deyman,
I think that I can safely say that nobody understands C++ template mechanics.)
$(P Templates are D's approach to generic programming.
Templates are defined with a $(I TemplateDeclaration):
)
$(GRAMMAR
$(GNAME TemplateDeclaration):
$(D template) $(I Identifier) $(GLINK TemplateParameters) $(GLINK Constraint)$(OPT) $(D {) $(GLINK2 module, DeclDefs)$(OPT) $(D })
$(GNAME TemplateParameters):
$(D $(LPAREN)) $(GLINK TemplateParameterList)$(OPT) $(D $(RPAREN))
$(GNAME TemplateParameterList):
$(GLINK TemplateParameter)
$(GLINK TemplateParameter) ,
$(GLINK TemplateParameter) , $(I TemplateParameterList)
$(GNAME TemplateParameter):
$(GLINK TemplateTypeParameter)
$(GLINK TemplateValueParameter)
$(GLINK TemplateAliasParameter)
$(GLINK TemplateSequenceParameter)
$(GLINK TemplateThisParameter)
)
$(P The body of the $(I TemplateDeclaration) must be syntactically correct
even if never instantiated. Semantic analysis is not done until
instantiated. A template forms its own scope, and the template
body can contain classes, structs, types, enums, variables,
functions, and other templates.
)
$(P Template parameters can be types, values, symbols, or sequences.
Types can be any type.
Value parameters must be of an integral type, floating point
type, or string type and
specializations for them must resolve to an integral constant,
floating point constant, null, or a string literal.
Symbols can be any non-local symbol.
Sequences can contain zero or more types, values or symbols.
)
$(P Template parameter specializations
constrain the values or types the $(I TemplateParameter) can
accept.
)
$(P Template parameter defaults are the value or type to use for the
$(I TemplateParameter) in case one is not supplied.
)
$(H2 $(LNAME2 explicit_tmp_instantiation, Explicit Template Instantiation))
$(P Templates are explicitly instantiated with:
)
$(GRAMMAR
$(GNAME TemplateInstance):
$(I Identifier) $(GLINK TemplateArguments)
$(GNAME TemplateArguments):
$(D ! $(LPAREN)) $(GLINK TemplateArgumentList)$(OPT) $(D $(RPAREN))
$(D !) $(GLINK TemplateSingleArgument)
$(GNAME TemplateArgumentList):
$(GLINK TemplateArgument)
$(GLINK TemplateArgument) ,
$(GLINK TemplateArgument) , $(I TemplateArgumentList)
$(GNAME TemplateArgument):
$(GLINK2 declaration, Type)
$(ASSIGNEXPRESSION)
$(GLINK Symbol)
$(GNAME Symbol):
$(GLINK SymbolTail)
$(D .) $(GLINK SymbolTail)
$(GNAME SymbolTail):
$(I Identifier)
$(I Identifier) $(D .) $(I SymbolTail)
$(GLINK TemplateInstance)
$(GLINK TemplateInstance) $(D .) $(I SymbolTail)
$(GNAME TemplateSingleArgument):
$(I Identifier)
$(GLINK2 declaration, BasicTypeX)
$(GLINK2 lex, CharacterLiteral)
$(GLINK2 lex, StringLiteral)
$(GLINK2 lex, IntegerLiteral)
$(GLINK2 lex, FloatLiteral)
$(D true)
$(D false)
$(D null)
$(D this)
$(GLINK2 traits, SpecialKeyword)
)
$(P Once instantiated, the declarations inside the template, called
the template members, are in the scope
of the $(I TemplateInstance):
------
template TFoo(T) { alias t = T*; }
...
TFoo!(int).t x; // declare x to be of type int*
------
)
$(P If the $(GLINK TemplateArgument) is one token long, the parentheses can be omitted:
---
TFoo!int.t x; // same as TFoo!(int).t x;
---
)
$(P A template instantiation can be aliased:
------
template TFoo(T) { alias t = T*; }
alias abc = TFoo!(int);
abc.t x; // declare x to be of type int*
------
)
$(P Multiple instantiations of a $(I TemplateDeclaration) with the same
$(I TemplateArgumentList) all will refer to the same instantiation.
For example:
------
template TFoo(T) { T f; }
alias a = TFoo!(int);
alias b = TFoo!(int);
...
a.f = 3;
assert(b.f == 3); // a and b refer to the same instance of TFoo
------
)
$(P This is true even if the $(I TemplateInstance)s are done in
different modules.
)
$(P Even if template arguments are implicitly converted to the same
template parameter type, they still refer to same instance:
-----
struct TFoo(int x) { }
// 3 and 2+1 are both 3 of type int
static assert(is(TFoo!(3) == TFoo!(2 + 1)));
// 3u is implicitly converted to 3 to match int parameter,
// and refers exactly same instance with TFoo!(3).
static assert(is(TFoo!(3) == TFoo!(3u)));
-----
)
$(P If multiple templates with the same $(I Identifier) are
declared, they are distinct if they have a different number of
arguments or are differently specialized.
)
$(P For example, a simple generic copy template would be:
------
template TCopy(T)
{
void copy(out T to, T from)
{
to = from;
}
}
------
)
$(P To use the template, it must first be instantiated with a specific
type:
------
int i;
TCopy!(int).copy(i, 3);
------
)
$(H2 $(LNAME2 instantiation_scope, Instantiation Scope))
$(P $(I TemplateInstantance)s are always performed in the scope of where
the $(I TemplateDeclaration) is declared, with the addition of the
template parameters being declared as aliases for their deduced types.
)
$(P For example:
$(BR)$(BR)
$(U module a)
------
template TFoo(T) { void bar() { func(); } }
------
$(U module b)
------
import a;
void func() { }
alias f = TFoo!(int); // error: func not defined in module a
------
)
$(P and:
$(BR)$(BR)
$(U module a)
------
template TFoo(T) { void bar() { func(1); } }
void func(double d) { }
------
$(U module b)
------
import a;
void func(int i) { }
alias f = TFoo!(int);
...
f.bar(); // will call a.func(double)
------
)
$(P $(I TemplateParameter) specializations and default
values are evaluated in the scope of the $(I TemplateDeclaration).
)
$(H2 Argument Deduction)
$(P The types of template parameters are deduced for a particular
template instantiation by comparing the template argument with
the corresponding template parameter.
)
$(P For each template parameter, the following rules are applied in
order until a type is deduced for each parameter:
)
$(OL
$(LI If there is no type specialization for the parameter,
the type of the parameter is set to the template argument.)
$(LI If the type specialization is dependent on a type parameter,
the type of that parameter is set to be the corresponding part
of the type argument.)
$(LI If after all the type arguments are examined there are any
type parameters left with no type assigned, they are assigned
types corresponding to the template argument in the same position
in the $(I TemplateArgumentList).)
$(LI If applying the above rules does not result in exactly one
type for each template parameter, then it is an error.)
)
$(P For example:
------
template TFoo(T) { }
alias Foo1 = TFoo!(int); // (1) T is deduced to be int
alias Foo2 = TFoo!(char*); // (1) T is deduced to be char*
template TBar(T : T*) { }
alias Foo3 = TBar!(char*); // (2) T is deduced to be char
template TAbc(D, U : D[]) { }
alias Bar1 = TAbc!(int, int[]); // (2) D is deduced to be int, U is int[]
alias Bar2 = TAbc!(char, int[]); // (4) error, D is both char and int
template TDef(D : E*, E) { }
alias Bar3 = TDef!(int*, int); // (1) E is int
// (3) D is int*
------
)
$(P Deduction from a specialization can provide values
for more than one parameter:
---
template Foo(T: T[U], U)
{
...
}
Foo!(int[long]) // instantiates Foo with T set to int, U set to long
---
)
$(P When considering matches, a class is
considered to be a match for any super classes or interfaces:
------
class A { }
class B : A { }
template TFoo(T : A) { }
alias Foo4 = TFoo!(B); // (3) T is B
template TBar(T : U*, U : A) { }
alias Foo5 = TBar!(B*, B); // (2) T is B*
// (3) U is B
------
)
$(H2 Template Type Parameters)
$(GRAMMAR
$(GNAME TemplateTypeParameter):
$(I Identifier)
$(I Identifier) $(GLINK TemplateTypeParameterSpecialization)
$(I Identifier) $(GLINK TemplateTypeParameterDefault)
$(I Identifier) $(GLINK TemplateTypeParameterSpecialization) $(GLINK TemplateTypeParameterDefault)
$(GNAME TemplateTypeParameterSpecialization):
$(D :) $(GLINK2 declaration, Type)
$(GNAME TemplateTypeParameterDefault):
$(D =) $(GLINK2 declaration, Type)
)
$(H3 $(LNAME2 parameters_specialization, Specialization))
$(P Templates may be specialized for particular types of arguments
by following the template parameter identifier with a : and the
specialized type.
For example:
------
template TFoo(T) { ... } // #1
template TFoo(T : T[]) { ... } // #2
template TFoo(T : char) { ... } // #3
template TFoo(T, U, V) { ... } // #4
alias foo1 = TFoo!(int); // instantiates #1
alias foo2 = TFoo!(double[]); // instantiates #2 with T being double
alias foo3 = TFoo!(char); // instantiates #3
alias fooe = TFoo!(char, int); // error, number of arguments mismatch
alias foo4 = TFoo!(char, int, int); // instantiates #4
------
)
$(P The template picked to instantiate is the one that is most specialized
that fits the types of the $(I TemplateArgumentList).
Determine which is more specialized is done the same way as the
C++ partial ordering rules.
If the result is ambiguous, it is an error.
)
$(H2 $(LNAME2 template_this_parameter, Template This Parameters))
$(GRAMMAR
$(GNAME TemplateThisParameter):
$(D this) $(I TemplateTypeParameter)
)
$(P $(I TemplateThisParameter)s are used in member function templates
to pick up the type of the $(I this) reference.
---
import std.stdio;
struct S
{
const void foo(this T)(int i)
{
writeln(typeid(T));
}
}
void main()
{
const(S) s;
(&s).foo(1);
S s2;
s2.foo(2);
immutable(S) s3;
s3.foo(3);
}
---
)
$(P Prints:
$(CONSOLE
const(S)
S
immutable(S)
)
)
$(P This is especially useful when used with inheritance. For example,
you might want to implement a final base method which returns a derived
class type. Typically you would return a base type, but this won't allow
you to call or access derived properties of the type:
---
interface Addable(T)
{
final auto add(T t)
{
return this;
}
}
class List(T) : Addable!T
{
List remove(T t)
{
return this;
}
}
void main()
{
auto list = new List!int;
list.add(1).remove(1); // error: no 'remove' method for Addable!int
}
---
)
$(P Here the method $(D add) returns the base type, which doesn't implement the
$(D remove) method. The $(D template this) parameter can be used for this purpose:
---
interface Addable(T)
{
final R add(this R)(T t)
{
return cast(R)this; // cast is necessary, but safe
}
}
class List(T) : Addable!T
{
List remove(T t)
{
return this;
}
}
void main()
{
auto list = new List!int;
list.add(1).remove(1); // ok
}
---
)
$(H2 $(LNAME2 template_value_parameter, Template Value Parameters))
$(GRAMMAR
$(GNAME TemplateValueParameter):
$(GLINK2 declaration, BasicType) $(GLINK2 declaration, Declarator)
$(GLINK2 declaration, BasicType) $(GLINK2 declaration, Declarator) $(GLINK TemplateValueParameterSpecialization)
$(GLINK2 declaration, BasicType) $(GLINK2 declaration, Declarator) $(GLINK TemplateValueParameterDefault)
$(GLINK2 declaration, BasicType) $(GLINK2 declaration, Declarator) $(GLINK TemplateValueParameterSpecialization) $(GLINK TemplateValueParameterDefault)
$(GNAME TemplateValueParameterSpecialization):
$(D :) $(GLINK2 expression, ConditionalExpression)
$(GNAME TemplateValueParameterDefault):
$(D =) $(ASSIGNEXPRESSION)
$(D =) $(GLINK2 traits, SpecialKeyword)
)
$(P Template value parameter types can be any type which can
be statically initialized at compile time.
Template value arguments can be integer values, floating point values,
nulls, string values, array literals of template value arguments,
associative array literals of template value arguments,
or struct literals of template value arguments.
-----
template foo(string s)
{
string bar() { return s ~ " betty"; }
}
void main()
{
writefln("%s", foo!("hello").bar()); // prints: hello betty
}
-----
)
$(P This example of template foo has a value parameter that
is specialized for 10:
------
template foo(U : int, int T : 10)
{
U x = T;
}
void main()
{
assert(foo!(int, 10).x == 10);
}
------
)
$(H2 $(LNAME2 aliasparameters, Template Alias Parameters))
$(GRAMMAR
$(GNAME TemplateAliasParameter):
$(D alias) $(I Identifier) $(GLINK TemplateAliasParameterSpecialization)$(OPT) $(GLINK TemplateAliasParameterDefault)$(OPT)
$(D alias) $(GLINK2 declaration, BasicType) $(GLINK2 declaration, Declarator) $(GLINK TemplateAliasParameterSpecialization)$(OPT) $(GLINK TemplateAliasParameterDefault)$(OPT)
$(GNAME TemplateAliasParameterSpecialization):
$(D :) $(GLINK2 declaration, Type)
$(D :) $(GLINK2 expression, ConditionalExpression)
$(GNAME TemplateAliasParameterDefault):
$(D =) $(GLINK2 declaration, Type)
$(D =) $(GLINK2 expression, ConditionalExpression)
)
$(P Alias parameters enable templates to be parameterized with
almost any kind of D symbol, including user-defined type names,
global names, local names, module names, template names, and
template instance names.
Literals can also be used as arguments to alias parameters.
)
$(P $(B Examples:))
$(UL
$(LI User-defined type names
------
class Foo
{
static int p;
}
template Bar(alias T)
{
alias q = T.p;
}
void test()
{
alias bar = Bar!(Foo);
bar.q = 3; // sets Foo.p to 3
}
------
)
$(LI Global names
------
int x;
template Foo(alias X)
{
static int* p = &X;
}
void test()
{
alias bar = Foo!(x);
*bar.p = 3; // set x to 3
static int y;
alias abc = Foo!(y);
*abc.p = 3; // set y to 3
}
------
)
$(LI Module names
------
import std.string;
template Foo(alias X)
{
alias y = X.toString;
}
void test()
{
alias bar = Foo!(std.string);
bar.y(3); // calls std.string.toString(3)
}
------
)
$(LI Template names
------
int x;
template Foo(alias X)
{
static int* p = &X;
}
template Bar(alias T)
{
alias abc = T!(x);
}
void test()
{
alias bar = Bar!(Foo);
*bar.abc.p = 3; // sets x to 3
}
------
)
$(LI Template instance names
------
int x;
template Foo(alias X)
{
static int* p = &X;
}
template Bar(alias T)
{
alias q = T.p;
}
void test()
{
alias foo = Foo!(x);
alias bar = Bar!(foo);
*bar.q = 3; // sets x to 3
}
------
)
$(LI Literals
------
template Foo(alias X, alias Y)
{
static int i = X;
static string s = Y;
}
void test()
{
alias foo = Foo!(3, "bar");
writeln(foo.i, foo.s); // prints 3bar
}
------
)
)
$(H3 $(LNAME2 typed_alias_op, Typed alias parameters))
$(P Alias parameters can also be typed.
These parameters will accept symbols of that type:
------
template Foo(alias int x) { }
int x;
float f;
Foo!x; // ok
Foo!f; // fails to instantiate
------
)
$(H3 $(LNAME2 alias_parameter_specialization, Specialization))
$(P Alias parameters can accept both literals and user-defined type symbols,
but they are less specialized than the matches to type parameters and
value parameters:
------
template Foo(T) { ... } // #1
template Foo(int n) { ... } // #2
template Foo(alias sym) { ... } // #3
struct S {}
int var;
alias foo1 = Foo!(S); // instantiates #1
alias foo2 = Foo!(1); // instantiates #2
alias foo3a = Foo!([1,2]); // instantiates #3
alias foo3b = Foo!(var); // instantiates #3
------
------
template Bar(alias A) { ... } // #4
template Bar(T : U!V, alias U, V...) { ... } // #5
class C(T) {}
alias bar = Bar!(C!int); // instantiates #5
------
)
$(H2 $(LNAME2 variadic-templates, Template Sequence Parameters))
$(GRAMMAR
$(GNAME TemplateSequenceParameter):
$(I Identifier) $(D ...)
)
$(P If the last template parameter in the $(I TemplateParameterList)
is declared as a $(I TemplateSequenceParameter),
it is a match with any trailing template arguments.
Such a sequence of arguments can be defined using the template
$(REF AliasSeq, std,meta) and will thus henceforth
be referred to by that name for clarity.
An $(I AliasSeq) is not itself a type, value, or symbol.
It is a compile-time sequence of any mix of types, values or symbols.
)
$(P An $(I AliasSeq) whose elements consist entirely of types is
called a type sequence or $(I TypeSeq).
An $(I AliasSeq) whose elements consist entirely of values is
called a value sequence or $(I ValueSeq).
)
$(P An $(I AliasSeq) can be used as an argument list to instantiate
another template, or as the list of parameters for a function.
---
template print(args...)
{
void print()
{
writeln("args are ", args); // args is a ValueSeq
}
}
template write(Args...)
{
void write(Args args) // Args is a TypeSeq
// args is a ValueSeq
{
writeln("args are ", args);
}
}
void main()
{
print!(1,'a',6.8).print(); // prints: args are 1a6.8
write!(int, char, double).write(1, 'a', 6.8); // prints: args are 1a6.8
}
---
)
$(P The number of elements in an $(I AliasSeq) can be retrieved with
the $(D .length) property. The $(I n)th element can be retrieved
by indexing the $(I AliasSeq) with [$(I n)],
and sub-sequences are denoted by the slicing syntax.
)
$(P $(I AliasSeq)-s are static compile-time entities, there is no way
to dynamically change, add, or remove elements either at compile-time or run-time.
)
$(P Type sequences can be deduced from the trailing parameters
of an implicitly instantiated function template:
---
template print(T, Args...)
{
void print(T first, Args args)
{
writeln(first);
static if (args.length) // if more arguments
print(args); // recurse for remaining arguments
}
}
void main()
{
print(1, 'a', 6.8);
}
---
)
$(P prints:
$(CONSOLE
1
a
6.8
)
)
$(P Type sequences can also be deduced from the type of a delegate
or function parameter list passed as a function argument:
----
import std.stdio;
/* Partially applies a delegate by tying its first argument to a particular value.
* R = return type
* T = first argument type
* Args = TypeSeq of remaining argument types
*/
R delegate(Args) partial(R, T, Args...)(R delegate(T, Args) dg, T first)
{
// return a closure
return (Args args) => dg(first, args);
}
void main()
{
int plus(int x, int y, int z)
{
return x + y + z;
}
auto plus_two = partial(&plus, 2);
writefln("%d", plus_two(6, 8)); // prints 16
}
----
See also: $(REF partial, std,functional)
)
$(H3 $(LNAME2 variadic_template_specialization, Specialization))
$(P If both a template with a sequence parameter and a template
without a sequence parameter exactly match a template instantiation,
the template without a $(I TemplateSequenceParameter) is selected.
----
template Foo(T) { pragma(msg, "1"); } // #1
template Foo(int n) { pragma(msg, "2"); } // #2
template Foo(alias sym) { pragma(msg, "3"); } // #3
template Foo(Args...) { pragma(msg, "4"); } // #4
import std.stdio;
// Any sole template argument will never match to #4
alias foo1 = Foo!(int); // instantiates #1
alias foo2 = Foo!(3); // instantiates #2
alias foo3 = Foo!(std); // instantiates #3
alias foo4 = Foo!(int, 3, std); // instantiates #4
----
)
$(H2 $(LNAME2 template_parameter_def_values, Template Parameter Default Values))
$(P Trailing template parameters can be given default values:
------
template Foo(T, U = int) { ... }
Foo!(uint,long); // instantiate Foo with T as uint, and U as long
Foo!(uint); // instantiate Foo with T as uint, and U as int
template Foo(T, U = T*) { ... }
Foo!(uint); // instantiate Foo with T as uint, and U as uint*
------
)
$(H2 $(LNAME2 implicit_template_properties, Eponymous Templates))
$(P If a template has exactly one member in it, and the name of that
member is the same as the template name, that member is assumed
to be referred to in a template instantiation:
------
template $(CODE_HIGHLIGHT Foo)(T)
{
T Foo; // declare variable Foo of type T
}
void test()
{
Foo!(int) = 6; // instead of Foo!(int).Foo
}
------
)
$(H2 $(LNAME2 template_ctors, Template Constructors))
$(GRAMMAR
$(GNAME ConstructorTemplate):
$(D this) $(GLINK2 template, TemplateParameters) $(GLINK2 function, Parameters) $(GLINK2 function, MemberFunctionAttributes)$(OPT) $(GLINK Constraint)$(OPT) $(D :)
$(D this) $(GLINK2 template, TemplateParameters) $(GLINK2 function, Parameters) $(GLINK2 function, MemberFunctionAttributes)$(OPT) $(GLINK Constraint)$(OPT) $(GLINK2 function, FunctionBody)
)
$(P Templates can be used to form constructors for classes and structs.
)
$(H2 $(LNAME2 aggregate_templates, Aggregate Templates))
$(GRAMMAR
$(GNAME ClassTemplateDeclaration):
$(D class) $(I Identifier) $(GLINK TemplateParameters) $(GLINK Constraint)$(OPT) $(GLINK2 class, BaseClassList)$(OPT) $(GLINK2 struct, AggregateBody)
$(D class) $(I Identifier) $(GLINK TemplateParameters) $(GLINK2 class, BaseClassList)$(OPT) $(GLINK Constraint)$(OPT) $(GLINK2 struct, AggregateBody)
$(GNAME InterfaceTemplateDeclaration):
$(D interface) $(I Identifier) $(GLINK TemplateParameters) $(GLINK Constraint)$(OPT) $(GLINK2 interface, BaseInterfaceList)$(OPT) $(GLINK2 struct, AggregateBody)
$(D interface) $(I Identifier) $(GLINK TemplateParameters) $(GLINK2 interface, BaseInterfaceList) $(GLINK Constraint) $(GLINK2 struct, AggregateBody)
$(GNAME StructTemplateDeclaration):
$(D struct) $(I Identifier) $(GLINK TemplateParameters) $(GLINK Constraint)$(OPT) $(GLINK2 struct, AggregateBody)
$(GNAME UnionTemplateDeclaration):
$(D union) $(I Identifier) $(GLINK TemplateParameters) $(GLINK Constraint)$(OPT) $(GLINK2 struct, AggregateBody)
)
$(P If a template declares exactly one member, and that member is a class
with the same name as the template:
------
template $(CODE_HIGHLIGHT Bar)(T)
{
class $(CODE_HIGHLIGHT Bar)
{
T member;
}
}
------
then the semantic equivalent, called a $(I ClassTemplateDeclaration)
can be written as:
------
class Bar(T)
{
T member;
}
------
)
$(P Analogously to class templates, struct, union and interfaces
can be transformed into templates by supplying a template parameter list.
)
$(H2 $(LNAME2 function-templates, Function Templates))
$(P If a template declares exactly one member, and that member is a function
with the same name as the template, it is a function template declaration.
Alternatively, a function template declaration is a function declaration
with a $(GLINK TemplateParameterList) immediately preceding the
$(GLINK2 function, Parameters).
)
$(P A function template to compute the square of type $(I T) is:
------
T $(CODE_HIGHLIGHT Square)(T)(T t)
{
return t * t;
}
------
)
$(P Function templates can be explicitly instantiated with a
!($(I TemplateArgumentList)):
----
writefln("The square of %s is %s", 3, Square!(int)(3));
----
)
$(P or implicitly, where the $(I TemplateArgumentList) is deduced
from the types of the function arguments:
----
writefln("The square of %s is %s", 3, Square(3)); // T is deduced to be int
----
)
$(P If there are fewer arguments supplied in the $(I TemplateArgumentList)
than parameters in the $(I TemplateParameterList), the arguments fulfill
parameters from left to right, and the rest of the parameters are then deduced
from the function arguments.
)
$(P Function template type parameters that are to be implicitly
deduced may not have specializations:
------
void $(CODE_HIGHLIGHT Foo)(T : T*)(T t) { ... }
int x,y;
Foo!(int*)(x); // ok, T is not deduced from function argument
Foo(&y); // error, T has specialization
------
)
$(P Template arguments not implicitly deduced can have default values:
------