Skip to content

Set/bitset examples should be more complete #453

@Moz-in-Aus

Description

@Moz-in-Aus

With the bitset enum, the example doesn't show how to convert to and from integers and I couldn't find any code online that does this. There's a to_ulong() function but I have no idea what the parameter for that is.

Isn't the whole point of a bitset enum that you can get and set integers? Otherwise it's just a set?

namespace foo { 
    enum class Color { RED = 1, GREEN = 2, BLUE = 4 };
} // namespace -> // is_flags has to be done from global namespace between definition and first use

template <>
struct magic_enum::customize::enum_range<Color> {
    static constexpr bool is_flags = true;
};

namespace foo {
    void main() {
        auto color_bitset = magic_enum::containers::bitset<Color>();
        color_bitset.set(3); 
        std::cout << color_bitset << std::endl;  // RED|GREEN
        int val = color_bitset.to_number(); // 3!
    }
} // namespace

That's my use case and why I looked more into magic_enum. Some embedded thing sends me a byte full of flags and I want to code as though what it meant was flags.test(Flags::SLOW_CLOCK) and reply with flags.set(Flags::FAST_CLOCK)?

More generally, it would be handy if the examples directory covered every public method on the type rather than just the trivial cases.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions