【发布时间】:2012-06-09 01:42:08
【问题描述】:
是否可以编写一个模板函数来增加不同结构的(数字)字段?例如:
struct Color
{
ubyte a,r,g,b;
}
struct Point
{
double x, y;
}
我尝试过这样的事情:
T update(T, A)(T t, A a)
if (is(T == struct))
{
auto vals = t.tupleof;
foreach (i; 0 .. vals.length) {
vals[i] += a; // error: i cannot be read at compile time
}
return T(vals); // convert back to struct
}
我也尝试过编写接受元组的函数模板,但是元组总是被扩展,这会阻止编译器匹配正确的模板。 谢谢。
【问题讨论】:
标签: templates d template-meta-programming