Template class container with automatic conversion task

I have the following code:

#include <vector> template<int Wt = 0> class fixed { public: explicit fixed(double val = 0) { operator=(val); } ~fixed(){} operator double() const { return v_; } double operator =(const double &d){ if (d>Wt) v_ = Wt; else v_ = d; return v_; } private: double v_; }; int main(){ fixed<5> x; std::vector<fixed<6> > v(5); //std::vector<fixed<6> > v(5,0); //fixed<6> y; //v[0] = 0; x = x*v[0]; } 

Compiling in VS 2005 Express and 2010 Express gives the following error:

error C2676: binary '*': 'fixed' does not define this operator or conversion to a type acceptable for a predefined operator

If I uncomment any of the three lines in the main one (commenting on an additional vector), it will compile. If I use gcc, it will compile. Can someone give a clue why this is?

The code is a simplified version of a larger project, so the three solutions, unfortunately, are not options for me.

+4
source share
1 answer

This seems to be a failure in vC ++. If I add a line

  x = x * (* & v [0]); 
BEFORE the line
  x = x * v [0]; 
(which caused the error), the error will disappear (I am using vc 2010 express). GCC compiles this code without errors, but only after renaming the class bound to something else (otherwise it complains about the ambiguity of this name, I don’t know why, maybe it also appears in some gcc headers)
+1
source

Source: https://habr.com/ru/post/1336430/


All Articles