enum_flags_name: return name for 0, if it exists#376
enum_flags_name: return name for 0, if it exists#376masbug wants to merge 1 commit intoNeargye:masterfrom
Conversation
|
Since this changes behavior it should probably be opt in |
|
I don't think it should be opt in. It fixes a bug. If 0 isn't defined as an enum value then it will still work as before. vs. |
I understand, but |
|
It already works for both cases. #include <magic_enum/magic_enum.hpp>
#include <iostream>
enum class E1 : int {
None = 0,
Flag1 = 1 << 0,
Flag2 = 2 << 1
};
int main() {
static_assert(magic_enum::enum_values<E1>()[0] == E1::None);
static_assert(magic_enum::enum_cast<E1>("None") == E1::None);
return 0;
} |
|
This is not a bitflag, you must do this template <>
struct magic_enum::customize::enum_range<E1> {
static constexpr bool is_flags = true;
};
// now your code failsotherwise you will be reflecting from It seems alot of people forget to set this boolean to true themselves, shouls it auto deduce? |
|
@ZXShady static_assert(magic_enum::enum_values<E1>()[0] == E1::Flag1);But this is missing: static_assert(magic_enum::enum_cast<E1>("None") == E1::None); |
|
There was some discussion about it including the 0 value. |
It would be great, but I haven't found a reliable way to detect that these are flags yet. Personally, I don't think zero should be included by default. I would prefer two separate options: flags and flags_with_zero (or flags_and_zero), though this is open for discussion. |
I do it myself by checking for & and | operators if they are overloaded. |
|
Could try adding auto detection. Then we definitely need swith using namespace magic_enum::bitwise_operators to enable_bitwise_operators = true 😁 |
Added constexpr that returns name for the value zero, if it exists.