I want to create a class, that will be implicitly convertible to selected enum classes, in order to switch over it. But following code does not compile (gcc). Compiler complains that conversion is ambiguous, but it does not compile with single conversion operator also (either one). How to implement such behaviour?
enum class G : int {
A,
B = 0x100,
};
enum class B : int {
B1 = 0x100,
B2
};
struct Foo {
int s;
operator G() {
return static_cast<G>(s & ~0xff);
}
operator B() {
return static_cast<B>(s);
}
};
void foo() {
Foo s;
s.s = static_cast<int>(B::B1);
switch (s) {
case G::A:
break;
default:
break;
}
switch (s) {
case B::B1:
break;
default:
break;
}
}