-
Notifications
You must be signed in to change notification settings - Fork 445
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
please add an ADL version of customize::enum_range<> (code included) #381
Comments
Correction: I was missing a Corrected
Corrected
|
Hi, I needed to adjust While doing that, I realized that we need to make sure ADL This is important in part because the existing
and so if we didn't make ADL take priority, then Actually, the definitions of So here is the code that handles It is much shorter and easier to understand than the old code because it uses I wrote some better documentation that you can use in your README (see comments):
Enjoy! |
The code above is almost all C++17 but I just realized that |
Hi, thanks so much for magic_enum!
As far as I can tell, the mechanism for turning on
is_flags
or otherenum_range
flags (specializingmagic_enum::customize::enum_range<T>
) only works from global scope (because C++ doesn't let us changemagic_enum::customize::
unless the scope we are in (global) enclosesmagic_enum
).This is quite inconvenient when the enum being customized is deep inside one or more classes and/or namespaces. And it's a little dangerous too, because if we accidentally use the enum with magic_enum before the specialization, magic_enum will do the wrong thing. It's good to have the magic_enum customize code right after where the enum is defined.
ADL is exactly the right tool for this because it can give magic_enum access to whatever scope contains the enum (even if it's deep inside namespaces and classes)
So, I have a simple proposal to add a second, ADL-based mechanism for magic_enum to find
enum_range
parameters.The idea is for magic_enum to also check for the ability to call the function
magic_enum_enum_range(t)
and if a call is possible, use the struct returned by the function in exactly the same way that magic_enum currently usesmagic_enum::customize::enum_range<T>
The function is never actually called at runtime (and it's constexpr) and it's never used in evaluated context, only used insidedecltype()
so it doesn't add any overhead.Here is the code for how to do this to fetch
is_flags
(tested on MSVC19.41 with-std:c++latest
, clang 18.0.2 with-std=c++2c
, gcc 13.1.0 with-std=c++20
)Here is the existing, unmodified code from magic_enum.hpp:
Add one more struct to try the ADL case:
Add this new
get_is_flags
struct to keep the code clean:and then later on where the code used to say:
change it to:
and similar small changes for the other
enum_range
flags likerange_min
andrange_max
Thanks!
The text was updated successfully, but these errors were encountered: