【发布时间】:2017-08-19 17:10:53
【问题描述】:
我正在尝试在 D 语言的 3D 向量之间实现诸如 +,-,*,/ 之类的算术运算(只是想知道从 C++ 迁移到 D 是否值得,我通常做 3D 图形和数值数学)。但是我可能遗漏了一些东西,下面的代码不起作用(我试图根据包含的参考资料来做)。
#!/usr/bin/env rdmd
import std.stdio;
// https://dlang.org/spec/operatoroverloading.html
// https://github.com/PhilippeSigaud/D-templates-tutorial/blob/master/D-templates-tutorial.md#u-op-v--------opbinarystring-s-vv-v-if-s--opv-op-u--------opbinaryrightstring-s-vv-v-if-s--op
struct vec3f{ float x,y,z; }
vec3f opBinary(string op)(vec3f a, vec3f b){
static if (op == "+"){ return vec3f(a.x+b.x,a.y+b.y,a.z+b.z); }
else static if (op == "*"){ return vec3f(a.x*b.x,a.y*b.y,a.z*b.z); }
else{ static assert(0, "Operator "~op~" not implemented"); }
}
struct vec3(T){ T x,y,z; }
auto opBinary(T,string op)(vec3!T a, vec3!T b){
static if (op == "+"){ return vec3!T(a.x+b.x,a.y+b.y,a.z+b.z); }
else static if (op == "*"){ return vec3!T(a.x*b.x,a.y*b.y,a.z*b.z); }
else{ static assert(0, "Operator "~op~" not implemented"); }
}
void main(){
//auto a = vec3!float(1.1,2.2,3.2);
//auto b = vec3!float(3.1,2.2,1.2);
auto a = vec3f(1.1,2.2,3.2);
auto b = vec3f(3.1,2.2,1.2);
writeln(a); writeln(b);
writeln( a+b );
}
opBinary 的实现似乎编译得很好,但是当我尝试使用它时总是出错:
./operator_overload.d(27): Error: incompatible types for ((a) + (b)): 'vec3f' and 'vec3f'
Failed: ["dmd", "-v", "-o-", "./operator_overload.d", "-I."]
编辑:我还尝试了 mixin 与 this answer 的匹配。同样的错误。
struct vec3f{ float x,y,z; }
vec3f opBinary(string op)(vec3f a,vec3f b)if(op=="+"||op=="-"||op=="*"||op=="/"){
mixin("return vec3f(a.x"~op~"b.x,a.y"~op~"b.y,a.z"~op~"b.z);");
}
编辑 2:是的,它必须是结构体的一部分(我不知道是否有任何方法可以使其成为独立功能)。这很完美:
#!/usr/bin/env rdmd
import std.stdio;
struct vec3(T){
float x,y,z;
vec3!T opBinary(string op)(vec3!T b) if(op=="+"||op=="-"||op=="*"||op=="/"){
mixin("return vec3!T(x"~op~"b.x,y"~op~"b.y,z"~op~"b.z);");
}
}
void main(){
auto a = vec3!float(1.1,2.2,3.2);
auto b = vec3!float(3.1,2.2,1.2);
writeln(a); writeln(b);
writeln( a+b );
writeln( a*b );
}
【问题讨论】:
-
我不确定,但是重载的运算符不需要是相应结构的成员吗?尝试将其移动到结构中(进行相关编辑,ofc)。
标签: templates struct operator-overloading d