Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- struct Vector(int size, T)
- {
- T[size] array;
- auto ref opBinary(string op)(T rhs) const
- if (op == "+" || op == "-" || op == "*" || op == "/")
- {
- Vector vector = this;
- mixin("return vector" ~ op ~ "= rhs;");
- }
- auto ref opOpAssign(string op)(T rhs)
- if (op == "+" || op == "-" || op == "*" || op == "/")
- {
- mixin("array[] " ~ op ~ "= rhs;");
- return this;
- }
- auto opOpAssign(string op)(const ref Vector rhs)
- // auto opOpAssign(string op)(const Vector rhs) // witouth ref everything works
- if (op == "+" || op == "-")
- {
- mixin("array[] " ~ op ~ "= rhs.array[];");
- return this;
- }
- }
- alias Vector!(3, float) Vec3f;
- import std.stdio;
- void main()
- {
- auto a = Vec3f([0, 0, 0]);
- a += a * 0;
- writeln(a.array); // prints random values like [-1.13483, 4.2039e-45, -1.13482]
- assert(a == Vec3f([0, 0, 0])); // fails
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement