mardi 14 avril 2020

c++ conversion operator overloading fails

I have code to overload the assignment operator for different types:

#include <string>
#include <iostream>

class MyClass {
   int m_int;
   double m_double;
   std::string m_string;

public:
   MyClass() : m_int{1}, m_double(1.2), m_string{"Test"} {};

   // overload the conversion operator for std::string
   operator std::string() {
      return m_string;
   }

   // overload the conversion operator for all other types
   template <typename T>
   operator T() {
      if (std::is_same<T, int>::value)
         return m_int;
      else
         return m_double;
   }
};

int main() {

   MyClass o;

   int i = o;
   double d = o;
   std::string s = o;

   std::cout << i << " " << " " << d << " " << s << std::endl;
}

This works correctly. But when I try do do

std::string s;
s = o;

the compilation fails with

error: use of overloaded operator '=' is ambiguous (with operand types 'std::string' (aka 'basic_string<char, char_traits<char>, allocator<char> >') and 'MyClass')

Apparently the compiler instantiates a conversion operator different than std::string(). If I remove the template section, the compiler is forced to use the std::string() conversion operator and it works again. So what am I missing?

Aucun commentaire:

Enregistrer un commentaire