不知道什么時候加入了“函數模板參數類型推導”和“類模板(改進型)”,文檔中也是悄悄出現了。
函數模板參數類型推導:
template Square(T)
{
T Square(T t)
{
return t * t;
}
}
以前調用時必須顯式實例化:
writefln("The square of %s is %s", 3, Square!(int)(3));
現在可以使用自動推導了:
writefln("The square of %s is %s", 3, Square(3)); // T is deduced to be int
類模板以前必須這樣寫:
template Bar(T)
{
class Bar
{
T member;
}
}
現在可以寫為:
class Bar(T)
{
T member;
}
含義相同。