Enum comparison without a + operator
yan-zaretskiy opened this issue · comments
Is it possible to avoid the plus operator for the comparisons between an Enum
and Enum::_enumerated
?
I tried to define
bool operator==(MyEnum left, MyEnum::_enumerated right) {
return left._to_integral() == right;
}
This seems to work. Am I thinking along the correct lines?
Every way I know (knew) of avoiding the +
operator had the side effect of introducing ambiguous overload resolution elsewhere. See comments here:
I grant that I may have missed something. Have you run the tests with your modification?
Hmm, trying to build tests complains about the absence of the cxxtest/tests.cc
file...
Nevermind, install cxxtest...
All errors I see are in the 101-special-values.cc
file where you define the invalid_t
and default_t
types, other tests can be built and they pass.
So, just for the reference, cause I don't really know what I'm doing, I added the following lines:
BETTER_ENUMS_UNUSED BETTER_ENUMS_CONSTEXPR_ \
inline bool operator ==(const Enum &a, const Enum::_enumerated &b) \
{ return a._to_integral() == b; } \
\
BETTER_ENUMS_UNUSED BETTER_ENUMS_CONSTEXPR_ \
inline bool operator ==(const Enum::_enumerated &a, const Enum &b) \
{ return a == b._to_integral(); } \
\
BETTER_ENUMS_UNUSED BETTER_ENUMS_CONSTEXPR_ \
inline bool operator !=(const Enum &a, const Enum::_enumerated &b) \
{ return a._to_integral() != b; } \
\
BETTER_ENUMS_UNUSED BETTER_ENUMS_CONSTEXPR_ \
inline bool operator !=(const Enum::_enumerated &a, const Enum &b) \
{ return a != b._to_integral(); } \