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

Feature: Check if a given value equals one of the enums #43

Closed
peter-moran opened this issue Dec 14, 2020 · 4 comments
Closed

Feature: Check if a given value equals one of the enums #43

peter-moran opened this issue Dec 14, 2020 · 4 comments

Comments

@peter-moran
Copy link

peter-moran commented Dec 14, 2020

I would like check if a value I have is equivalent to one of my enums.

For example, assume a function contains, I would expect it to behave as:

WISE_ENUM(Color, (GREEN, 2), RED);

static_assert(wise_enum::contains<Color>(1) == false);
static_assert(wise_enum::contains<Color>(2) == true);
static_assert(wise_enum::contains<Color>(3) == true);
@quicknir
Copy link
Owner

@peter-moran That's pretty easy to write yourself. You can use wise enum to loop over the entries and check, and if you want write a small helper function to encapsulate it. But it's not something I'm likely to merge in. Generally things that are both easy to implement and can easily be implemented "outside" the repo aren't likely candidates; everyone will have their own preferred API/approach for these things and it's pretty low value. The main goal of the repo is to do the "hard" stuff of emulating reflection so users don't have to.

@peter-moran
Copy link
Author

That is a good point, but that would incur a linear search when an auto generated switch statement would be O(1).

Is the string conversion handled as a switch statement or a linear search?

@quicknir
Copy link
Owner

Enum to string is handled as a switch case. String to enum is handled as linear search. The enum to string switch case is disabled if two enumerators have the same value (because it's ambiguous).

An auto generated switch case isn't necessarily going to be more efficient, it really depends. Linear search will be pretty good for small enums, if you want it to be fast the best of course is to ensure contiguous values and just do the two comparisons. Beyond that, if your enum is truly large with values all over the place then a hash table is probably your best bet.

This feature could probably be implemented efficiently via my switch case "lift" notion.

@peter-moran
Copy link
Author

Thanks, I’ll just go with using the linear search then. Interesting idea on the switch case lifts. Thanks for a great library!

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