root/trunk/docsrc/template-comparison.dd

Revision 1378, 11.3 kB (checked in by walter, 2 years ago)

bugzilla 3584 DeclDef? rule is missing entries

  • Property svn:eol-style set to native
Line 
1 Ddoc
2
3 $(COMMUNITY Template Comparison,
4
5 $(P C++ pioneered templates and template metaprogramming, and continues
6 to improve on it with C++0x.
7 The D programming language is the first to comprehensively reengineer
8 templates based on the C++ experience.
9 Since C++0x is not a ratified standard yet, proposed changes to C++
10 are subject to change.)
11
12     <table border=2 cellpadding=4 cellspacing=0 class="comp">
13     <caption>Template Comparison Table</caption>
14
15     <thead>
16     $(TR
17     $(TH Feature)
18     $(TH D)
19     $(TH C++98)
20     $(TH C++0x)
21     )
22     </thead>
23
24     <tbody>
25
26     $(TR
27     $(TD Argument list delineation)
28     $(TD Uses !( ), as in Foo!(int))
29     $(TD Uses &lt; &gt; as in Foo&lt;int&gt;)
30     $(TD No change)
31     )
32
33     $(TR
34     $(TD Class Templates)
35     $(TD Yes:
36 ---
37 class Foo(T)
38 {
39   T x;
40 }
41 ---
42 )
43     $(TD Yes:
44 $(CPPCODE2
45 template&lt;class T&gt;
46   class Foo
47 {
48   T x;
49 };
50 )
51 )
52     $(TD No change)
53     )
54
55     $(TR
56     $(TD Function Templates)
57     $(TD Yes:
58 ---
59 T foo(T)(T i)
60 {
61   ...
62 }
63 ---
64 )
65     $(TD Yes:
66 $(CPPCODE2
67 template&lt;class T&gt;
68   T foo(T i)
69 {
70   ...
71 }
72 )
73 )
74     $(TD No change)
75     )
76
77     $(TR
78     $(TD Member Templates)
79     $(TD Yes)
80     $(TD Yes)
81     $(TD No change)
82     )
83
84     $(TR
85     $(TD Constructor Templates)
86     $(TD No)
87     $(TD Yes)
88     $(TD No change)
89     )
90
91     $(TR
92     $(TD Parameterize any Declaration)
93     $(TD Yes, classes, functions, typedefs,
94     variables, enums, etc. can be parameterized,
95     such as this variable:
96 ---
97 template Foo(T)
98 {
99   static T* p;
100 }
101 ---
102 )
103     $(TD No, only classes and functions)
104     $(TD No change)
105     )
106
107     $(TR
108     $(TD Template Typedefs: Create an alias that binds to some but not all
109     of the template parameters)
110     $(TD Yes:
111 ---
112 class Foo(T, U) { }
113 template MyFoo(T)
114 {
115   alias Foo!(T, int) MyFoo;
116 }
117 MyFoo!(uint) f;
118 ---
119 )
120     $(TD No)
121     $(TD Yes:
122 $(CPPCODE2
123 template&lt;class T, class U&gt; class Foo { };
124 template&lt;class T&gt; using MyFoo = Foo&lt;T, int&gt;;
125 MyFoo&lt;unsigned&gt; f;
126 )
127 )
128     )
129
130
131     $(TR
132     $(TD Sequence Constructors)
133     $(TD No)
134     $(TD No)
135     $(TD Yes:
136 $(CPPCODE2
137 Foo&lt;double&gt; f = { 1.2, 3, 6.8 };
138 )
139 )
140     )
141
142     $(TR
143     $(TD Concepts)
144 $(V1
145     $(TD No, but much the same effect can be achieved with
146      $(LINK2 version.html#staticif, static if) and
147      $(LINK2 version.html#StaticAssert, static asserts))
148 )
149 $(V2
150     $(TD Yes: $(LINK2 cpp0x.html#concepts, Constraints))
151 )
152     $(TD No)
153     $(TD Yes: $(LINK2 http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1849.pdf, Concepts for C++0x N1849))
154     )
155
156
157     $(TR
158     $(TD Recursive Templates)
159     $(TD Yes:
160 ---
161 template factorial(int n)
162 {
163   const factorial =
164      n * factorial!(n-1);
165 }
166 template factorial(int n : 1)
167 {
168   const factorial = 1;
169 }
170 ---
171 )
172     $(TD Yes:
173 $(CPPCODE2
174 template&lt;int n&gt; class factorial
175 {
176   public:
177     enum
178     {
179       result =
180          n * factorial&lt;n-1&gt;::result
181     };
182 };
183 template&lt;&gt; class factorial&lt;1&gt;
184 {
185   public:
186     enum { result = 1 };
187 };
188 )
189 )
190     $(TD No change)
191     )
192
193     $(TR
194     $(TD Conditional Compilation based on
195     Template Arguments)
196     $(TD Yes:
197 ---
198 template factorial(int n)
199 {
200   static if (n == 1)
201     const factorial = 1;
202   else
203     const factorial =
204        n * factorial!(n-1);
205 }
206 ---
207 )
208     $(TD No:
209 $(CPPCODE2
210 template&lt;int n&gt; class factorial
211 {
212   public:
213     enum
214     {
215 #if (n == 1) // $(ERROR)
216       result = 1;
217 #else
218       result =
219          n * factorial&lt;n-1&gt;::result
220 #endif
221     };
222 };
223 )
224 )
225     $(TD No change)
226     )
227
228     $(TR
229     $(TD Template Declarations (with no definition))
230     $(TD No)
231     $(TD Yes:
232 $(CPPCODE2
233 template&lt;class T&gt;
234   class Foo;
235 )
236 )
237     $(TD No change)
238     )
239
240     $(TR
241     $(TD Grouping templates with the same parameters together)
242     $(TD Yes:
243 ---
244 template Foo(T, U)
245 {
246   class Bar { ... }
247   T foo(T t, U u) { ... }
248 }
249 Foo!(int,long).Bar b;
250 return Foo!(char,int).foo('c',3);
251 ---
252 )
253     $(TD No, each must be separate:
254 $(CPPCODE2
255 template&lt;class T, class U&gt;
256   class Foo_Bar { ... };
257 template&lt;class T, class U&gt;
258   T Foo_foo(T t, U u) { ... };
259 Foo_Bar&lt;int,long&gt; b;
260 return Foo_foo&lt;char,int&gt('c',3);
261 )
262 )
263     $(TD No change)
264     )
265
266     $(TR
267     $(TD Compile time execution of functions)
268     $(TD $(LINK2 function.html#interpretation, Yes):
269 ---
270 int factorial(int i)
271 { if (i == 0)
272     return 1;
273   else
274     return i * factorial(i - 1);
275 }
276 static f = factorial(6);
277 ---
278     )
279     $(TD No)
280     $(TD Named constant expressions with parameters:
281        $(LINK2 http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/n1972.pdf, Generalized Constant Expressions N1972))
282     )
283
284     $(TR
285     $(TH Parameters)
286     $(TH D)
287     $(TH C++98)
288     $(TH C++0x)
289     )
290
291     $(TR
292     $(TD Type Parameters)
293     $(TD Yes:
294 ---
295 class Foo(T)
296 {
297   T x;
298 }
299 Foo!(int) f;
300 ---
301 )
302     $(TD Yes:
303 $(CPPCODE2
304 template&lt;class T&gt;
305   class Foo
306 {
307   T x;
308 };
309 Foo&lt;int&gt; f;
310 )
311 )
312     $(TD No change)
313     )
314
315     $(TR
316     $(TD Integral Parameters)
317     $(TD Yes:
318 ---
319 void foo(int i)()
320 {
321   int v = i;
322 }
323 ---
324 )
325     $(TD Yes:
326 $(CPPCODE2
327 template&lt;int i&gt;
328     void foo()
329 {
330   int v = i;
331 }
332 )
333 )
334     $(TD No change)
335     )
336
337     $(TR
338     $(TD Pointer Parameters)
339     $(TD Yes, a pointer to object or function)
340     $(TD Yes, a pointer to object or function)
341     $(TD No change)
342     )
343
344     $(TR
345     $(TD Reference Parameters)
346     $(TD No, D does not have a general reference type)
347     $(TD Yes:
348 $(CPPCODE2
349 template&lt;double& D&gt;
350     void foo()
351 {
352   double y = D;
353 }
354 )
355 )
356     $(TD No change)
357     )
358
359     $(TR
360     $(TD Pointer to Member Parameters)
361     $(TD No, D does not have pointers to members, it has
362       $(LINK2 type.html#delegates, delegates),
363       which can be used as parameters)
364     $(TD Yes)
365     $(TD No change)
366     )
367
368     $(TR
369     $(TD Template Template Parameters)
370     $(TD Yes:
371 ---
372 class Foo(T, alias C)
373 {
374   C!(T) x;
375 }
376 ---
377 )
378     $(TD Yes:
379 $(CPPCODE2
380 template&lt;class T,
381          template&lt;class U&gt; class C&gt;
382     class Foo
383 {
384   C&lt;T&gt; x;
385 };
386 )
387 )
388     $(TD No change)
389     )
390
391     $(TR
392     $(TD Alias Parameters)
393     $(TD Yes, any symbol can be passed to a template as an alias:
394 ---
395 void bar(int);
396 void bar(double);
397 void foo(T, alias S)(T t)
398 {
399   S(t);
400 }
401 // calls bar(double)
402 foo!(double, bar)(1);
403 ---
404 )
405     $(TD No)
406     $(TD No change)
407     )
408
409     $(TR
410     $(TD Floating Point Parameters)
411     $(TD Yes:
412 ---
413 class Foo(double D)
414 {
415   double x = D;
416 }
417 ...
418 Foo!(1.6) F;
419 ---
420 )
421     $(TD No)
422     $(TD No change)
423     )
424
425     $(TR
426     $(TD String Parameters)
427     $(TD Yes:
428 ---
429 void foo(char[] format)(int i)
430 {
431   writefln(format, i);
432 }
433 ...
434 foo!("i = %s")(3);
435 ---
436 )
437     $(TD No)
438     $(TD No change)
439     )
440
441     $(TR
442     $(TD Local Class Parameters)
443     $(TD Yes)
444     $(TD No)
445     $(TD Issue N1945)
446     )
447
448     $(TR
449     $(TD Local Variable Parameters)
450     $(TD Yes)
451     $(TD No)
452     $(TD No change)
453     )
454
455     $(TR
456     $(TD Parameter Default Values)
457     $(TD Yes:
458 ---
459 class Foo(T = int)
460 {
461   T x;
462 }
463 ---
464 )
465     $(TD Yes:
466 $(CPPCODE2
467 template&lt;class T = int&gt;
468   class Foo
469 {
470   T x;
471 };
472 )
473 )
474     $(TD No change)
475     )
476
477     $(TR
478     $(TD Variadic Parameters)
479     $(TD Yes, $(LINK2 variadic-function-templates.html, Variadic Templates):
480 ---
481 void print(A...)(A a)
482 {
483     foreach(t; a)
484     writefln(t);
485 }
486 ---
487 )
488     $(TD No)
489     $(TD $(LINK2 http://www.osl.iu.edu/~dgregor/cpp/variadic-templates.pdf, Variadic Templates N2080))
490     )
491
492     $(TR
493     $(TH Specializations)
494     $(TH D)
495     $(TH C++98)
496     $(TH C++0x)
497     )
498
499     $(TR
500     $(TD Explicit Specialization)
501     $(TD Yes:
502 ---
503 class Foo(T : int)
504 {
505   T x;
506 }
507 ---
508 )
509     $(TD Yes:
510 $(CPPCODE2
511 template&lt;&gt;
512   class Foo&lt;int&gt;
513 {
514   int x;
515 };
516 )
517 )
518     $(TD No change)
519     )
520
521     $(TR
522     $(TD Partial Specialization)
523     $(TD Yes:
524 ---
525 class Foo(T : T*, U)
526 {
527   T x;
528 }
529 ---
530 )
531     $(TD Yes:
532 $(CPPCODE2
533 template&lt;class T, class U&gt;
534   class Foo&lt;T*, U&gt;
535 {
536   T x;
537 };
538 )
539 )
540     $(TD No change)
541     )
542
543     $(TR
544     $(TD Partial specialization derived from multiple parameters)
545     $(TD Yes:
546 ---
547 class Foo(T : Bar!(T, U), U)
548 {
549   ...
550 }
551 ---
552 )
553     $(TD Yes:
554 $(CPPCODE2
555 template&lt;class T, class U&gt;
556     class Foo&lt; Bar&lt;T,U&gt; &gt;
557 {
558   ...
559 };
560 )
561 )
562     $(TD No change)
563     )
564
565     $(TR
566     $(TD Can specializations exist without a primary template?)
567     $(TD Yes)
568     $(TD No)
569     $(TD No change)
570     )
571
572     $(TR
573     $(TH Other)
574     $(TH D)
575     $(TH C++98)
576     $(TH C++0x)
577     )
578
579     $(TR
580     $(TD Exported Templates)
581     $(TD Yes, it falls out as a natural consequence of modules)
582     $(TD Yes, though only in compilers based on EDG's front end)
583     $(TD No change)
584     )
585
586     $(TR
587     $(TD $(SFINAE))
588     $(TD Yes)
589     $(TD Yes)
590     $(TD No change)
591     )
592
593     $(TR
594     $(TD Parse Template Definition Bodies before Instantiation)
595     $(TD Yes)
596     $(TD Not required by Standard, but some implementations do)
597     $(TD No change)
598     )
599
600     $(TR
601     $(TD Overloading Function Templates with Functions)
602     $(TD No, but the equivalent can be done with explicitly specialized
603     templates:
604 ---
605 void foo(T)(T t) { }
606 void foo(T:int)(int t) { }
607 ---
608 )
609     $(TD Yes:
610 $(CPPCODE2
611 template&lt;class T&gt;
612   void foo(T i) { }
613 void foo(int t) { }
614 )
615 )
616     $(TD No change)
617     )
618
619     $(TR
620     $(TD Implicit Function Template Instantiation)
621     $(TD Yes)
622     $(TD Yes)
623     $(TD No change)
624     )
625
626     $(TR
627     $(TD Templates can be evaluated in scope
628       of instantiation rather than definition)
629     $(TD Yes, $(LINK2 mixin.html, Mixins))
630     $(TD No, but can be faked using macros)
631     $(TD No change)
632     )
633
634     $(TR
635     $(TH Parsing Idiosyncracies)
636     $(TH D)
637     $(TH C++98)
638     $(TH C++0x)
639     )
640
641
642
643     $(TR
644     $(TD Context-Free Grammar)
645     $(TD Yes:
646 ---
647 class Foo!(int i)
648 {
649    ...
650 }
651 Foo!(3 $(B >) 4) f;
652 ---
653 )
654     $(TD No:
655 $(CPPCODE2
656 template&lt;int i&gt; class Foo
657 {
658    ...
659 };
660 Foo&lt;3 $(B &gt;) 4&gt; f; // $(ERROR)
661 )
662 )
663     $(TD No change)
664     )
665
666
667     $(TR
668     $(TD Distinguish template arguments from other operators)
669     $(TD Yes:
670 ---
671 class Foo!(T)
672 {
673    ...
674 }
675 class Bar!(int i)
676 {
677    ...
678 }
679 Foo!(Bar!(1)) x1;
680 ---
681 )
682     $(TD No:
683 $(CPPCODE2
684 template&lt;class T&gt; class Foo
685 {
686    ...
687 };
688 template&lt;int i&gt; class Bar
689 {
690    ...
691 };
692 Foo&lt;Bar&lt;1&gt;&gt; x1; // $(ERROR)
693 Foo&lt;Bar&lt;1&gt; &gt; x2;
694 )
695 )
696     $(TD Partially fixed by
697     $(LINK2 http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2005/n1757.html, Right Angle Brackets N1757)
698     )
699
700     )
701
702
703     $(TR
704     $(TD Redeclaration of Template Parameter)
705     $(TD Yes:
706 ---
707 class Foo(T)
708 {
709   int T;
710   void foo()
711   {
712     int T;
713   }
714 }
715 ---
716 )
717     $(TD No:
718 $(CPPCODE2
719 template&lt;class T&gt;
720   class Foo
721 {
722   int T; // $(ERROR)
723   void foo()
724   {
725     int T; // $(ERROR)
726   }
727 };
728 )
729 )
730     $(TD No change)
731     )
732
733     $(TR
734     $(TD Dependent Base Class Lookup)
735     $(TD Yes:
736 ---
737 class Foo(T)
738 {
739   typedef int $(B A);
740 }
741 class Bar(T) : Foo(T)
742 {
743   $(B A) x;
744 }
745 ---
746 )
747     $(TD No:
748 $(CPPCODE2
749 template&lt;class T&gt;
750   class Foo
751 {
752   public:
753     typedef int $(B A);
754 };
755 template&lt;class T&gt;
756   class Bar : Foo&lt;T&gt;
757 {
758   public:
759     $(B A) x; // $(ERROR)
760 };
761 )
762 )
763     $(TD No change)
764     )
765
766
767
768
769     $(TR
770     $(TD Forward Referencing)
771     $(TD Yes:
772 ---
773 int $(B g)(void *);
774
775 class Foo(T)
776 {
777   int foo()
778   {
779     return $(B g)(1);
780   }
781 }
782
783 int $(B g)(int i);
784 ---
785 )
786     $(TD No:
787 $(CPPCODE2
788 int $(B g)(void *);
789
790 template&lt;class T&gt;
791   class Foo
792 {
793   int foo()
794   {
795     return $(B g)(1); // $(ERROR)
796   }
797 };
798
799 int $(B g)(int i);
800 )
801 )
802     $(TD No change)
803     )
804
805
806     $(TR
807     $(TD Member templates parseable without hints)
808     $(TD Yes:
809 ---
810 class Foo
811 {
812     Foo bar!(int I)();
813 }
814 void abd(T)(T f)
815 {
816   T f1 = f.bar!(3)();
817 }
818 ---
819 )
820     $(TD No:
821 $(CPPCODE2
822 class Foo
823 {
824   public:
825     template&lt;int&gt; Foo *bar();
826 };
827 template&lt;class T&gt; void abc(T *f)
828 {
829   T *f1 = f-&gt;bar&lt;3&gt;(); // $(ERROR)
830   T *f2 = f-&gt;$(B template) bar&lt;3&gt;();
831 }
832 )
833 )
834     $(TD No change)
835     )
836
837
838     $(TR
839     $(TD Dependent type members parseable without hints)
840     $(TD Yes:
841 ---
842 class Foo(T)
843 {
844   T.A* a1;
845 }
846 ---
847 )
848     $(TD No:
849 $(CPPCODE2
850 template<class T> class Foo
851 {
852   public:
853     T::A *a1; // $(ERROR)
854     $(B typename) T::A *a2;
855 };
856 )
857 )
858     $(TD No change)
859     )
860
861     </tbody>
862     </table>
863
864 )
865
866 Macros:
867     TITLE=Template Comparison
868     WIKI=TemplateComparison
869     NO=<td class="compNo">No</td>
870     NO1=<td class="compNo"><a href="$1">No</a></td>
871     YES=<td class="compYes">Yes</td>
872     YES1=<td class="compYes"><a href="$1">Yes</a></td>
873     D_CODE = <pre class="d_code2">$0</pre>
874     CPPCODE2 = <pre class="cppcode2">$0</pre>
875     ERROR = $(RED $(B error))
876 META_KEYWORDS=D Programming Language, template metaprogramming,
877 variadic templates, type deduction, dependent base class
878 META_DESCRIPTION=Comparison of templates between the
879 D programming language, C++, and C++0x
Note: See TracBrowser for help on using the browser.