enum to string c++17 Magic Enum

Magic Enum header-only library provides static reflection for enums (to string, from string, iteration) for C++17.

c++17提供了将enum想String的转换

#include 

enum Color { RED = 2, BLUE = 4, GREEN = 8 };

Color color = Color::RED;
auto color_name = magic_enum::enum_name(color);
// color_name -> "RED"

std::string color_name{"GREEN"};
auto color = magic_enum::enum_cast(color_name)
if (color.has_value()) {
  // color.value() -> Color::GREEN
};

你可能感兴趣的:(C/C++)