struct Vec(E, size_t n) {
E[n] impl;
alias impl this;
E[n] opBinary(string op)(Vec v) {
Vec result;
mixin("result.impl[] = impl[] "~op~" v.impl[];");
return result;
}
}
auto v(Args...)(Args args) {
import std.traits : CommonType;
alias E = CommonType!Args;
Vec!(E, Args.length) result;
result.impl = [ args ];
return result;
}
extern(C) void main() @nogc {
// Look, ma! No allocations!
int[3] arr = v(1,2,3) + v(4,5,6);
assert(arr[0] == 5 && arr[1] == 7 && arr[2] == 9);
}
d不需要分配
原创
©著作权归作者所有:来自51CTO博客作者fqbqrr的原创作品,请联系作者获取转载授权,否则将追究法律责任
上一篇:d的betterC版造转换矩
下一篇:d生成重载函数
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
写程序不需要天份,也不需要热情
。这种深植人心的刻板印象不
PHP Java 程序语言