2012-12-15 205 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