Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

template template parameter

Tags:

d

Does D support template template parameters? How would I get the following to work?

struct Type(alias a, alias b) { alias a A; alias b B; }

template MakeType(alias a, alias b)
{
  alias Type!(a, b) MakeType;
}

template Foo(alias a, U) // where U is a Type
{
  //...
}

template Foo(alias a, U : MakeType!(a, b), b...)  // where U is a specialization
{
  //...
}

and Foo is supposed to be called as such:

alias MakeType!(5, 7) MyType;
alias Foo!(5, MyType) Fooed;  // error

Error: template instance Foo!(5,Type!(5,7)) Foo!(5,Type!(5,7)) does not match template declaration Foo(alias a,U : MakeType!(a,b),b...)

like image 972
Arlen Avatar asked Dec 18 '25 14:12

Arlen


1 Answers

I got it working :-)

template Foo(alias a, U) // where U is a Type
{
  //...
}
template Foo(alias a, U : X, X) if(is(X == MakeType!(a, U.B)))
{
  //...
}

and in usage:

alias MakeType!(1, 3) MyType1;
alias MakeType!(5, 7) MyType2;
Foo!(5, MyType1) // calls the first Foo()
Foo!(5, MyType2) // calls the second Foo() with specialization
like image 127
Arlen Avatar answered Dec 20 '25 17:12

Arlen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!