There is a class in a very simplified form that looks like this:
class variant { public: variant (){}; template <class T> operator T () { return T(11); } operator string () { string str = "sss"; return str; } };
and there is a program:
int main() { variant v1; int x1 = v1; // тут все хорошо (вызывается operator int ()) x1 = v1; // тут тоже все хорошо (вызывается так же operator int ()) variant v2; string s1 = v2; // тут все хорошо (вызывается operator string ()) s1 = v2; // тут получаю ошибку компиляции return 0; }
I can't understand what's wrong, if I set the value of the string
variable during initialization, everything is fine, but if I try to assign a value to the string
variable, I get an error (with the built-in data types, everything is fine)
error: ambiguous overload for 'operator=' (operand types are 'std::__cxx11::string {aka std::__cxx11::basic_string<char>}' and 'variant')
what am I doing wrong? How to overload something like a string operator= (variant)
?
compiler: gcc version 5.1.0 (MinGW-W64 project)