Skip to content

Latest commit

 

History

History
115 lines (88 loc) · 4.06 KB

limitations.md

File metadata and controls

115 lines (88 loc) · 4.06 KB

Limitations

  • This library uses a compiler-specific hack (based on __PRETTY_FUNCTION__ / __FUNCSIG__), which works on Clang >= 5, MSVC >= 15.3 and GCC >= 9.

  • To check is magic_enum supported compiler use macro MAGIC_ENUM_SUPPORTED or constexpr constant magic_enum::is_magic_enum_supported.
    If magic_enum used on unsupported compiler, occurs the compilation error. To suppress error define macro MAGIC_ENUM_NO_CHECK_SUPPORT.

  • Enum can't reflect if the enum is a forward declaration.

  • For enum-flags add is_flags to specialization enum_range for necessary enum type. Specialization of enum_range must be injected in namespace magic_enum::customize.

    enum class Directions { Up = 1 << 1, Down = 1 << 2, Right = 1 << 3, Left = 1 << 4 };
    template <>
    struct magic_enum::customize::enum_range<Directions> {
      static constexpr bool is_flags = true;
    };
  • Enum value must be in range [MAGIC_ENUM_RANGE_MIN, MAGIC_ENUM_RANGE_MAX].

    • By default MAGIC_ENUM_RANGE_MIN = -128, MAGIC_ENUM_RANGE_MAX = 128.

    • MAGIC_ENUM_RANGE = (MAGIC_ENUM_RANGE_MAX - MAGIC_ENUM_RANGE_MIN) must be less than UINT16_MIN.

    • If need another range for all enum types by default, redefine the macro MAGIC_ENUM_RANGE_MIN and MAGIC_ENUM_RANGE_MAX.

      #define MAGIC_ENUM_RANGE_MIN 0
      #define MAGIC_ENUM_RANGE_MAX 256
      #include <magic_enum.hpp>
    • If need another range for specific enum type, add specialization enum_range for necessary enum type. Specialization of enum_range must be injected in namespace magic_enum::customize.

      #include <magic_enum.hpp>
      
      enum class number { one = 100, two = 200, three = 300 };
      
      template <>
      struct magic_enum::customize::enum_range<number> {
        static constexpr int min = 100;
        static constexpr int max = 300;
        // (max - min) must be less than UINT16_MAX.
      };
  • magic_enum won't work if a value is aliased. Work with enum-aliases is compiler-implementation-defined.

    enum ShapeKind {
      ConvexBegin = 0,
      Box = 0, // Won't work.
      Sphere = 1,
      ConvexEnd = 2,
      Donut = 2, // Won't work too.
      Banana = 3,
      COUNT = 4
    };
    // magic_enum::enum_cast<ShapeKind>("Box") -> nullopt
    // magic_enum::enum_name(ShapeKind::Box) -> "ConvexBegin"

    One of the possible workaround the issue:

    enum ShapeKind {
      // Convex shapes, see ConvexBegin and ConvexEnd below.
      Box = 0,
      Sphere = 1,
    
      // Non-convex shapes.
      Donut = 2,
      Banana = 3,
    
      COUNT = Banana + 1,
    
      // Non-reflected aliases.
      ConvexBegin = Box,
      ConvexEnd = Sphere + 1
    };
    // magic_enum::enum_cast<ShapeKind>("Box") -> ShapeKind::Box
    // magic_enum::enum_name(ShapeKind::Box) -> "Box"
    
    // Non-reflected aliases.
    // magic_enum::enum_cast<ShapeKind>("ConvexBegin") -> nullopt
    // magic_enum::enum_name(ShapeKind::ConvexBegin) -> "Box"

    On some compiler enum-aliases not supported, for example Visual Studio 2017, macro MAGIC_ENUM_SUPPORTED_ALIASES will be undefined.

    enum Number {
      one = 1,
      ONE = 1
    };
    // magic_enum::enum_cast<Number>("one") -> nullopt
    // magic_enum::enum_name(Number::one) -> ""
    // magic_enum::enum_cast<Number>("ONE") -> nullopt
    // magic_enum::enum_name(Number::ONE) -> ""
  • If you hit a message like this:

    [...]
    note: constexpr evaluation hit maximum step limit; possible infinite loop?
    

    Change the limit for the number of constexpr evaluated:

  • Intellisense Visual Studio may have some problems analyzing magic_enum.