I have written a simple Flags
class, but face a problem with my operator definition. It seems like I'm relying on some nonexistent implicit conversion rule.
enum class SomeEnum { ONE, TWO, THREE };
template<typename Enum>
class Flags {
Flags(const Flags& f) { };
Flags(Enum e) { };
Flags& operator |=(Flags<Enum> f) {
//...;
return *this;
}
};
template<typename Enum>
Flags<Enum> operator |(Enum a, Flags<Enum> b) { return b | a; }
template<typename Enum>
Flags<Enum> operator |(Flags<Enum> a, Enum b) { return a |= b; }
int main() {
Flags<SomeEnum> flags = SomeEnum::ONE | SomeEnum::TWO;
return 0;
}
While compilation I get this error:
implicit.cpp: In function ‘int main()’:
implicit.cpp:26:40: error: no match for ‘operator|’ (operand types are ‘SomeEnum’ and ‘SomeEnum’)
Flags<SomeEnum> flags = SomeEnum::ONE | SomeEnum::TWO;
My understanding was, that one of the SomeEnum
s is implicitly converted into a Flags<Enum>
, and then passed to the correct operator. What rule am I missing?
EDIT:
I have looked at https://stackoverflow.com/questions/9787593
, but the proposed solution (nonmember-friend operator) did not solve my problem.
I did remove the global definitions and add these members:
friend Flags operator |(Enum a, Flags b) { return b | a; }
friend Flags operator |(Flags a, Enum b) { return a |= b; }
But the error is still the same (live demo).