I am trying to understand why my C ++ compiler is confused with the following code fragment:
struct Enum { enum Type { T1, T2 }; Enum( Type t ):t_(t){} operator Type () const { return t_; } private: Type t_;
The selection leads to:
$ c++ enum.cxx enum.cxx: In function 'int main()': enum.cxx:30:10: error: ambiguous overload for 'operator==' (operand types are 'Enum' and 'Enum') b = t1 == t2; ^ enum.cxx:30:10: note: candidates are: enum.cxx:30:10: note: operator==(Enum::Type, Enum::Type) <built-in> enum.cxx:30:10: note: operator==(int, int) <built-in>
I understand that I can solve the symptoms by providing an explicit operator== :
bool operator==(Enum const &rhs) { return t_ == rhs.t_; }
But in fact, what I'm looking for is an interpretation of why enum comparisons only lead to ambiguity when they are executed within the class . I wrote this little enum wrapper since I only need to use C ++ 03 in my code.
source share