Advertisement
Guest User

Untitled

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