Movatterモバイル変換


[0]ホーム

URL:


www.digitalmars.com

D Programming Language 1.0


Last update Sun Dec 30 20:34:43 2012

Books

Template Comparison

C++ pioneered templates and template metaprogramming, and continuesto improve on it with C++0x.The D programming language is the first to comprehensively reengineertemplates based on the C++ experience.Since C++0x is not a ratified standard yet, proposed changes to C++are subject to change.

Template Comparison Table
FeatureDC++98C++0x
Argument list delineationUses !( ), as in Foo!(int)Uses < > as in Foo<int>No change
Class TemplatesYes:
class Foo(T){  T x;}
Yes:
template<class T>  class Foo{  T x;};
No change
Function TemplatesYes:
T foo(T)(T i){  ...}
Yes:
template<class T>  T foo(T i){  ...}
No change
Member TemplatesYesYesNo change
Constructor TemplatesNoYesNo change
Parameterize any DeclarationYes, classes, functions, typedefs,variables, enums, etc. can be parameterized,such as this variable:
template Foo(T){static T* p;}
No, only classes and functionsNo change
Template Typedefs: Create an alias that binds to some but not allof the templateparametersYes:
class Foo(T, U) { }template MyFoo(T){alias Foo!(T,int) MyFoo;}MyFoo!(uint) f;
NoYes:
template<class T, class U> class Foo { };template<class T> using MyFoo = Foo<T, int>;MyFoo<unsigned> f;
Sequence ConstructorsNoNoYes:
Foo<double> f = { 1.2, 3, 6.8 };
ConceptsNo, but much the same effect can be achieved withStaticIfCondition andstatic assertsNoYes:Concepts for C++0x N1849
Recursive TemplatesYes:
template factorial(int n){const factorial =     n * factorial!(n-1);}template factorial(int n : 1){const factorial = 1;}
Yes:
template<int n> class factorial{  public:    enum    {      result =         n * factorial<n-1>::result    }; };template<> class factorial<1>{  public:    enum { result = 1 };};
No change
Conditional Compilation based onTemplate ArgumentsYes:
template factorial(int n){staticif (n == 1)const factorial = 1;elseconst factorial =       n * factorial!(n-1);}
No:
template<int n> class factorial{  public:    enum    {#if (n == 1) //error      result = 1;#else      result =         n * factorial<n-1>::result#endif    }; };
No change
Template Declarations (with no definition)NoYes:
template<class T>  class Foo;
No change
Grouping templates with the same parameters togetherYes:
template Foo(T, U){class Bar { ... }  T foo(T t, U u) { ... }}Foo!(int,long).Bar b;return Foo!(char,int).foo('c',3);
No, each must be separate:
template<class T, class U>  class Foo_Bar { ... };template<class T, class U>  T Foo_foo(T t, U u) { ... };Foo_Bar<int,long> b;return Foo_foo<char,int>('c',3);
No change
Compile time execution of functionsYes:
int factorial(int i){if (i == 0)return 1;elsereturn i * factorial(i - 1);}static f = factorial(6);
NoNamed constant expressions with parameters:Generalized Constant Expressions N1972
ParametersDC++98C++0x
Type ParametersYes:
class Foo(T){  T x;}Foo!(int) f;
Yes:
template<class T>  class Foo{  T x;};Foo<int> f;
No change
Integral ParametersYes:
void foo(int i)(){int v = i;}
Yes:
template<int i>    void foo(){  int v = i;}
No change
Pointer ParametersYes, a pointer to object or functionYes, a pointer to object or functionNo change
Reference ParametersNo, D does not have a general reference typeYes:
template<double& D>    void foo(){  double y = D;}
No change
Pointer to Member ParametersNo, D does not have pointers to members, it hasdelegates, which can be used as parametersYesNo change
Template Template ParametersYes:
class Foo(T,alias C){  C!(T) x;}
Yes:
template<class T,         template<class U> class C>    class Foo{  C<T> x;};
No change
Alias ParametersYes, any symbol can be passed to a template as an alias:
void bar(int);void bar(double);void foo(T,alias S)(T t){  S(t);}// calls bar(double)foo!(double, bar)(1);
NoNo change
Floating Point ParametersYes:
class Foo(double D){double x = D;}...Foo!(1.6) F;
NoNo change
String ParametersYes:
void foo(char[] format)(int i){  writefln(format, i);}...foo!("i = %s")(3);
NoNo change
Local Class ParametersYesNoIssue N1945
Local Variable ParametersYesNoNo change
Parameter Default ValuesYes:
class Foo(T =int){  T x;}
Yes:
template<class T = int>  class Foo{  T x;};
No change
Variadic ParametersYes,Variadic Templates:
void print(A...)(A a){foreach(t; a)writefln(t);}
NoVariadic Templates N2080
SpecializationsDC++98C++0x
Explicit SpecializationYes:
class Foo(T :int){  T x;}
Yes:
template<>  class Foo<int>{  int x;};
No change
Partial SpecializationYes:
class Foo(T : T*, U){  T x;}
Yes:
template<class T, class U>  class Foo<T*, U>{  T x;};
No change
Partial specialization derived from multiple parametersYes:
class Foo(T : Bar!(T, U), U){  ...}
Yes:
template<class T, class U>    class Foo< Bar<T,U> >{  ...};
No change
Can specializations exist without a primary template?YesNoNo change
OtherDC++98C++0x
Exported TemplatesYes, it falls out as a natural consequence of modulesYes, though only in compilers based on EDG's front endNo change
SFINAE (Substitution Failure Is Not An Error)YesYesNo change
Parse Template Definition Bodies before InstantiationYesNot required by Standard, but some implementations doNo change
Overloading Function Templates with FunctionsNo, but the equivalent can be done with explicitly specializedtemplates:
void foo(T)(T t) { }void foo(T:int)(int t) { }
Yes:
template<class T>  void foo(T i) { }void foo(int t) { }
No change
Implicit Function Template InstantiationYesYesNo change
Templates can be evaluated in scope of instantiation rather than definitionYes,MixinsNo, but can be faked using macrosNo change
Parsing IdiosyncraciesDC++98C++0x
Context-Free GrammarYes:
class Foo!(int i){   ...}Foo!(3> 4) f;
No:
template<int i> class Foo{   ...};Foo<3> 4> f; //error
No change
Distinguish template arguments from other operatorsYes:
class Foo!(T){   ...}class Bar!(int i){   ...}Foo!(Bar!(1)) x1;
No:
template<class T> class Foo{   ...};template<int i> class Bar{   ...};Foo<Bar<1>> x1; //errorFoo<Bar<1> > x2;
Partially fixed byRight Angle Brackets N1757
Redeclaration of Template ParameterYes:
class Foo(T){int T;void foo()  {int T;  }}
No:
template<class T>  class Foo{  int T; //error  void foo()  {    int T; //error  }};
No change
Dependent Base Class LookupYes:
class Foo(T){typedefintA;}class Bar(T) : Foo(T){A x;}
No:
template<class T>  class Foo{  public:    typedef intA;};template<class T>  class Bar : Foo<T>{  public:A x; //error};
No change
Forward ReferencingYes:
intg(void *);class Foo(T){int foo()  {returng(1);  }}intg(int i);
No:
intg(void *);template<class T>  class Foo{  int foo()  {    returng(1); //error  }};intg(int i);
No change
Member templates parseable without hintsYes:
class Foo{    Foo bar!(int I)();}void abd(T)(T f){  T f1 = f.bar!(3)();}
No:
class Foo{  public:    template<int> Foo *bar();};template<class T> void abc(T *f){  T *f1 = f->bar<3>(); //error  T *f2 = f->template bar<3>();}
No change
Dependent type members parseable without hintsYes:
class Foo(T){  T.A* a1;}
No:
template class Foo{  public:    T::A *a1; //errortypename T::A *a2;};
No change




Forums |Comments | D  |Search |Downloads |Home
Copyright © 1999-2012 by Digital Mars ®, All Rights Reserved |Page generated byDdoc.

[8]ページ先頭

©2009-2026 Movatter.jp