Templated struct operator overloading

Peter Alexander peter.alexander.au at gmail.com
Fri Aug 6 12:54:51 PDT 2010


Is this a bug, or a limitation of how operator overloads handle generic 
operands?

   struct Vec(int N) {}

   struct Mat(int M, int N)
   {
       Vec!(M) opBinary(string op)(in Vec!(N) rhs)
       {
           return Vec!(M)();
       }
   }

   void main()
   {
       Vec!(3) v;
       Mat!(3, 3) m;

       Vec!(3) x = m * v; // Error: incompatible types for m * v
       Vec!(3) y = m.opBinary("*")(v); // Works
   }

Note that if you change the operator signature to:

   Vec!(3) opBinary(string op)(in Vec!(3) rhs)

then the first statement also compiles, but of course this makes the 
program incorrect.

Is this a bug or a language limitation?


More information about the Digitalmars-d mailing list