2009-11-13
D言語にはコンストラクタテンプレートがない。
string wstring dstring 版の
3つのコンストラクタをもつクラス。
class Foo { this(string c){...} this(wstring w){...} this(dstring d){...} }
関数テンプレートのように定義してみる。
class Foo { this(T)(T s){...} }
auto a = new Foo("bar"w);// Error: no constructor for Foo
ドキュメントに載っている通りエラーになる。
かわりに、
クラステンプレートを使ってみる。
class Foo(T) { this(T s){...} }
auto foo = new Foo!(wstring)("bar"w);//wstring 版の Foo のコンストラクタ
どうせなら、クラステンプレートの引数は、
コンストラクタの引数から推論してほしい。
あー
クラステンプレートとコンストラクタテンプレートでは、
意味合いが違うか。
クラステンプレートの場合は、
テンプレート引数ごとに違う型になる。