Skip to content

Conversation

@masbug
Copy link

@masbug masbug commented Sep 4, 2024

Added constexpr that returns name for the value zero, if it exists.

@ZXShady
Copy link
Contributor

ZXShady commented Oct 4, 2025

Since this changes behavior it should probably be opt in

@masbug
Copy link
Author

masbug commented Oct 9, 2025

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.

// this works same as before
enum E1 {
    A = 1<<0,
    B = 2<<1
}

vs.

// this will now resolve value 0 to "None"
enum E2 {
    None = 0,
    A = 1<<0,
    B = 2<<1
}```

@ZXShady
Copy link
Contributor

ZXShady commented Oct 9, 2025

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.

// this works same as before
enum E1 {
    A = 1<<0,
    B = 2<<1
}

vs.

// this will now resolve value 0 to "None"
enum E2 {
    None = 0,
    A = 1<<0,
    B = 2<<1
}```

I understand, but magic_enum::enum_values()[0] isn't 0 either, so this seems inconsistent to only apply it here, how about enum_cast<Bitflag>("None") ?

@masbug
Copy link
Author

masbug commented Oct 9, 2025

It already works for both cases.
See this test https://godbolt.org/z/nEKoP6j4n

#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;
}

@ZXShady
Copy link
Contributor

ZXShady commented Oct 9, 2025

@masbug

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 fails

otherwise you will be reflecting from [-128,127] instead of N^2

@Neargye

It seems alot of people forget to set this boolean to true themselves, shouls it auto deduce?

@masbug
Copy link
Author

masbug commented Oct 9, 2025

@ZXShady
Thanks for pointing that out. I see now what you mean.
I suppose this is the correct behavior for flags, since 0 is a special state.

static_assert(magic_enum::enum_values<E1>()[0] == E1::Flag1);

But this is missing:

static_assert(magic_enum::enum_cast<E1>("None") == E1::None);

@ZXShady
Copy link
Contributor

ZXShady commented Oct 10, 2025

There was some discussion about it including the 0 value.

#314

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

Successfully merging this pull request may close these issues.

2 participants