-
-
Notifications
You must be signed in to change notification settings - Fork 173
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
Support using better-enum as key in dictionaries and maps. #77
Conversation
This enabled hashing so that unordered_map are supported.
My previous attempt does not work and I totally did not think about that situation that a better enum could be in a namespace. Then the specialization does fail. However, how about that approach? namespace test {
BETTER_ENUM(Channel, char, Red = 1, Green, Blue)
}
BETTER_ENUMS_DECLARE_STD_HASH(test::Channel)
int main()
{
test::Channel channel = test::Channel::Red;
std::unordered_map<test::Channel, int> u = {
{test::Channel::Red, 1},
{test::Channel::Blue, 3},
};
printf("Red %d\n", u[test::Channel::Red]);
printf("Blue %d\n", u[test::Channel::Blue]);
return 0;
} |
enum.h
Outdated
@@ -1283,4 +1283,17 @@ BETTER_ENUMS_CONSTEXPR_ map<Enum, T> make_map(T (*f)(Enum)) | |||
|
|||
} | |||
|
|||
#ifndef BETTER_ENUMS_DECLARE_STD_HASH |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This check shouldn't be necessary.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup, will handle that.
enum.h
Outdated
{ \ | ||
size_t operator()(const type &x) const \ | ||
{ \ | ||
return std::hash<size_t>()(x._to_index()); \ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why use the index rather than the value? _to_index
runs a loop internally, whereas _to_integral
is just a cast. Is this deliberate?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes indeed. I am going to update that to use _to_integral
.
If it works, I will merge it :) If possible, can you add a test case that exercises this? It should probably go somewhere in this block: better-enums/test/cxxtest/general.h Lines 30 to 32 in 8a0d376
|
Do you mean by making a test case that tests the |
Yes. |
I am going to add the test case on Monday. |
I have added a new runtime test @aantron as I dont know if it is really possible to make a |
A run-time test is fine. It looks like it's not compiling, though. See, e.g., https://travis-ci.org/aantron/better-enums/jobs/558780607#L1386. |
Wow, finally.. @aantron ^^ |
:p Thanks! |
Thanks for better-enum. It is awesome. However I was missing a feature to use the enum values in unordered_map. I have extended it for this use case. Please review and accept my PR.