Skip to content

Working as intended? Named combined bitflags cannot be casted to #378

@JWCS

Description

@JWCS

I believe this is related to #314 and #321, so my apologies if it is. But, from the docs and those comments, it almost seemed like the below use case should work; but it fails for the BOTH case. This is after I considered your casting comments in 314 about enum vs enum_flags, and accounted for both input formats, to try both in what seemed the least restrictive way. I also read through the limitations... so I am very confused.
Working off of head (0.9.6):

#include <magic_enum_flags.hpp>
#include <iostream>
#include <string>

enum class FlowDirection : unsigned {
    NONE  = 0,
    CCW   = 1,
    CW    = 2,
    BOTH  = 3,
};
template <>
struct magic_enum::customize::enum_range<FlowDirection> {
  static constexpr bool is_flags = true;
};
inline static FlowDirection FlowDirection_read(
    std::string const& str, FlowDirection const& def = FlowDirection::NONE)
{
  return magic_enum::enum_flags_cast<FlowDirection>(str, magic_enum::case_insensitive).value_or(
      magic_enum::enum_cast<FlowDirection>(str, magic_enum::case_insensitive).value_or(def));
}

static void test(std::string const& str){
    FlowDirection flow = FlowDirection_read(str);
    std::cout << str << " ("
      << magic_enum::enum_name(flow) << "/"
      << magic_enum::enum_flags_name(flow) << "): "
      << magic_enum::enum_integer(flow) << std::endl;
}

int main(){
    test("CCW");
    test("ccw");
    test("cw");
    test("BOTH");
    test("NONE");
    test("NONE|CCW");
    test("CCW|CW");
    return 0;
}

Which results in incorrect values definitely for BOTH, and a curious non-parsing (?) for "NONE|CCW"

> g++ -std=gnu++17 demo.cpp && ./a.out
CCW (CCW/CCW): 1    # OK
ccw (CCW/CCW): 1    # OK
cw (CW/CW): 2       # OK
BOTH (/): 0         # Uhhhhh help?? I have no clue what I'm doing wrong, to not get this.
NONE (/): 0         # This is interesting; the value is correct, but the name is missing; which might be 321
NONE|CCW (/): 0     # Uh, I don't think I need this, but, is this a bug?
CCW|CW (/CCW|CW): 3 # OK

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions