From 9786f361c22da50521480d642396e7620bae9f2f Mon Sep 17 00:00:00 2001 From: neargye Date: Mon, 8 Apr 2019 18:30:13 +0500 Subject: [PATCH] add remarks about alias --- README.md | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/README.md b/README.md index 980e32b..cf36f95 100644 --- a/README.md +++ b/README.md @@ -190,7 +190,45 @@ enum Color { RED = 2, BLUE = 4, GREEN = 8 }; static constexpr int max = 300; }; } + ``` +* `magic_enum::enum_cast` and `magic_enum::enum_name` obtains the first defined value enums, and won't work if value are aliased. + ```cpp + 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("Box") -> std::nullopt + // magic_enum::enum_name(ShapeKind::Box) -> "ConvexBegin" + ``` + Work around the issue: + ```cpp + 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("Box") -> ShapeKind::Box + // magic_enum::enum_name(ShapeKind::Box) -> "Box" + + // Non-reflected aliases. + // magic_enum::enum_cast("ConvexBegin") -> std::nullopt + // magic_enum::enum_name(ShapeKind::ConvexBegin) -> "Box" ``` ## Integration