Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enum comparison without a + operator #82

Open
yan-zaretskiy opened this issue Feb 25, 2020 · 5 comments
Open

Enum comparison without a + operator #82

yan-zaretskiy opened this issue Feb 25, 2020 · 5 comments

Comments

@yan-zaretskiy
Copy link

Is it possible to avoid the plus operator for the comparisons between an Enum and Enum::_enumerated?

@yan-zaretskiy
Copy link
Author

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?

@aantron
Copy link
Owner

aantron commented Feb 25, 2020

Every way I know (knew) of avoiding the + operator had the side effect of introducing ambiguous overload resolution elsewhere. See comments here:

#23 (comment)
#13 (comment)

I grant that I may have missed something. Have you run the tests with your modification?

@yan-zaretskiy
Copy link
Author

yan-zaretskiy commented Feb 25, 2020

Hmm, trying to build tests complains about the absence of the cxxtest/tests.cc file...
Nevermind, install cxxtest...

@yan-zaretskiy
Copy link
Author

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.

@yan-zaretskiy
Copy link
Author

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(); }                                          \

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants