2012-12-15 45 views
3

D是否支持模板模板参数?我将如何得到以下工作?模板模板参数

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 
{ 
    //... 
} 

Foo应该被称为例如:

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...)

+0

注意:在我的实际情况中,'a'和'b'不是简单的整数值;它们是用户定义类型的实例。 – Arlen

+0

这里的目标是让b从其余的参数填充到MakeType,对吧?你可以手动检查:“template Foo(alias a,U,b ...)if(is(U == MakeType!(a,b)))”与“别名Foo!(5,MyType,7) Fooed;”但是和和b都是明确给出的。 现在,如果MyType有一个类型为b的成员,那么您可以很容易地将其解析出来,而不是作为模板参数,但与成员一样。像这样:http://arsdnet.net/thingy.d –

+0

@ AdamD.Ruppe我不能明确给出'a'和'b'。 MyType可以有'a'和'b'的别名,我改变了代码以反映这一点。另外请注意,'Foo'中的'U'是一个专门化。 – Arlen

回答

5

我得到它的工作:-)

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

和用法:

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