Consider following class used to wrap enum
class A {
int v;
A(int v) : v(v) { }
public:
enum B : int { X = 1, Y = 2, Z = 3 };
A(B v) : A(static_cast<int>(v)) { }
A operator|(const A &b) const { return v | b.v; }
operator bool() const { return v; }
};
int main() {
A a(A::Y);
if (a)
return 1;
a = a | A::X; // this line failes to compile
return 0;
}
I get an error on the marked line about ambigous overload
1.cpp:15:11: note: candidates are:
1.cpp:15:11: note: operator|(int, int) <built-in>
1.cpp:7:7: note: A A::operator|(const A&) const
If i comment out the bool cast operator the ambigous overload error goes away, so I suggest ambiguity arose from following casting path
(A)a -[operator]-> (bool)a -[implicit?]-> (int)a
Is there a way to resolve this ambiguity? The bool operator is used to allow
if (a) ...
and I'd rather stick to it than use something like
if (!!a) ...
with operator!().