Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2014
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
D 0.84 KB | None | 0 0
  1. import std.stdio;
  2.  
  3. void main()
  4. {
  5.     auto a = Vec3f([0, 0, 0]);
  6.     a += a * 0;
  7.     writeln(a.array); // prints random values like [-1.13483, 4.2039e-45, -1.13482]
  8.     assert(a == Vec3f([0, 0, 0])); // fails
  9. }
  10.  
  11. alias Vector!(3, float) Vec3f;
  12. struct Vector(int size, T)
  13. {
  14.     T[size] array;
  15.  
  16.     auto ref opBinary(string op)(T rhs) const
  17.     if (op == "+" || op == "-" || op == "*" || op == "/")
  18.     {
  19.         Vector vector = this;
  20.         mixin("return vector" ~ op ~ "= rhs;");
  21.     }
  22.  
  23.     auto ref opOpAssign(string op)(T rhs)
  24.     if (op == "+" || op == "-" || op == "*" || op == "/")
  25.     {
  26.         mixin("array[] " ~ op ~ "= rhs;");
  27.         return this;
  28.     }
  29.  
  30.     auto opOpAssign(string op)(const ref Vector rhs)
  31.     // auto opOpAssign(string op)(const Vector rhs) // witouth ref everything works
  32.     if (op == "+" || op == "-")
  33.     {
  34.         mixin("array[] " ~ op ~ "= rhs.array[];");
  35.         return this;
  36.     }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement