www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Template Arguments

reply Xinok <xnknet gmail.com> writes:
Is it possible to create template parameters in the D language?
template <template <class T> class T> class N;

I think this would be the most obvious syntax, but it doesn't seem to work:
template temp(template T(T1)){ }


My second question, is it possible to specialize by template arguments?
template temp(T : T!(T1 : int))
Jan 09 2007
parent reply Don Clugston <dac nospam.com.au> writes:
Xinok wrote:
 Is it possible to create template parameters in the D language?
 template <template <class T> class T> class N;
 
 I think this would be the most obvious syntax, but it doesn't seem to work:
 template temp(template T(T1)){ }

Too complicated, and not powerful enough! template temp(alias T) { T!(T1); } alias parameters are a generalisation of template template parameters. T1 doesn't have to be a template, just anything that has a name.
 My second question, is it possible to specialize by template arguments?
 template temp(T : T!(T1 : int))

Yes. template temp(alias T1: T!(int) ) { } And you can have default alias template arguments, too.
Jan 09 2007
parent Xinok <xnknet gmail.com> writes:
Don Clugston Wrote:

 template temp(alias T) {
     T!(T1);
 }
 
 alias parameters are a generalisation of template template parameters.
 T1 doesn't have to be a template, just anything that has a name.

I see template parameters as a 'self documenting' feature, so the developer knows two things: 1) The template is looking for a template 2) The dev knows what arguments the template needs If you apply a template to an alias, you can basically make any template compatible with the template argument: template a(int V, T){ } template b(template T(T, int V)){ } template al(T, int V){ alias a!(V, T) al; } b!(al) // Makes template 'b' compatible with template 'a' And regardless if you use an alias or template argument, you'd still have to define template 'al'.
 Yes.
 
 template temp(alias T1: T!(int) )
 {
 }
 
 And you can have default alias template arguments, too.
 

I tried the following code: template temp(alias T){ } template temp(alias T : T!(int)){ } And got the error: template main.temp(alias T : T!(int)) conflicts with main.temp(alias T) at main.d(4)
Jan 09 2007