在现代C ++ 11 / C ++ 14 / C ++ 17和未来的C ++ 20中枚举为字符串 [英] enum to string in modern C++11 / C++14 / C++17 and future C++20

查看:179
本文介绍了在现代C ++ 11 / C ++ 14 / C ++ 17和未来的C ++ 20中枚举为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  • 2008 c Is there a simple way to convert C++ enum to string?
  • 2008 c Easy way to use variables of enum types as string in C?
  • 2008 c++ How to easily map c++ enums to strings
  • 2008 c++ Making something both a C identifier and a string?
  • 2008 c++ Is there a simple script to convert C++ enum to string?
  • 2009 c++ How to use enums as flags in C++?
  • 2011 c++ How to convert an enum type variable to a string?
  • 2011 c++ Enum to String C++
  • 2011 c++ How to convert an enum type variable to a string?
  • 2012 c How to convert enum names to string in c
  • 2013 c Stringifying an conditionally compiled enum in C

  • 使用C ++ 11 , C ++ 14 C ++ 17 新功能

  • 或其他可以立即使用的东西在 Boost

  • 还有其他计划的< a href = https://en.wikipedia.org/wiki/C%2B%2B20 rel = noreferrer> C ++ 20

  • Elegant way using C++11, C++14 or C++17 new features
  • Or something ready-to-use in Boost
  • Else something planned for C++20

一个示例通常比一个长的ex更好

您可以在 Coliru 上编译并运行此代码段。

另一个以前的示例也可用)

An example is often better than a long explanation.
You can compile and run this snippet on Coliru.
(Another former example is also available)

#include <map>
#include <iostream>

struct MyClass
{
    enum class MyEnum : char {
        AAA = -8,
        BBB = '8',
        CCC = AAA + BBB
    };
};

// Replace magic() by some faster compile-time generated code
// (you're allowed to replace the return type with std::string
// if that's easier for you)
const char* magic (MyClass::MyEnum e)
{
    const std::map<MyClass::MyEnum,const char*> MyEnumStrings {
        { MyClass::MyEnum::AAA, "MyClass::MyEnum::AAA" },
        { MyClass::MyEnum::BBB, "MyClass::MyEnum::BBB" },
        { MyClass::MyEnum::CCC, "MyClass::MyEnum::CCC" }
    };
    auto   it  = MyEnumStrings.find(e);
    return it == MyEnumStrings.end() ? "Out of range" : it->second;
}

int main()
{
   std::cout << magic(MyClass::MyEnum::AAA) <<'\n';
   std::cout << magic(MyClass::MyEnum::BBB) <<'\n';
   std::cout << magic(MyClass::MyEnum::CCC) <<'\n';
}


约束



  • 其他答案基本链接没有无价值的重复。

  • 请避免基于宏的膨胀,或者尝试尽量减少 #define 的开销。

  • 请不要手动枚举-> 字符串映射。

  • Constraints

    • Please no valueless duplication of other answers or basic link.
    • Please avoid bloat macro-based answer, or try to reduce the #define overhead as minimum as possible.
    • Please no manual enum -> string mapping.

      • 支持 enum 值,该值从零开始的数字

      • 支持负数 enum

      • 支持分段的枚举

      • 支持 class枚举(C ++ 11)

      • 支持 class枚举:< type> 具有任何允许的< type> (C ++ 11)

      • 将编译时(而非运行时)转换为字符串,

        或至少在运行时快速执行(例如, std :: map 并不是一个好主意...)

      • constexpr (C ++ 11,然后在C ++ 14/17/20中放松)

      • noexcept (C ++ 11)

      • C ++ 17 / C ++ 20 友好代码段

      • Support enum values starting from a number different from zero
      • Support negative enum values
      • Support fragmented enum values
      • Support class enum (C++11)
      • Support class enum : <type> having any allowed <type> (C++11)
      • Compile-time (not run-time) conversions to a string,
        or at least fast execution at run-time (e.g. std::map is not a great idea...)
      • constexpr (C++11, then relaxed in C++14/17/20)
      • noexcept (C++11)
      • C++17/C++20 friendly snippet

      一个可能的想法可能是使用C ++编译器功能在编译时使用基于可变模板类和<$ c $的元编程技巧来生成C ++代码。 c> constexpr 函数...

      One possible idea could be using the C++ compiler capabilities to generate C++ code at compilation-time using meta-programming tricks based on variadic template class and constexpr functions...

      推荐答案

      魔术枚举仅标头库为C ++ 17的枚举(从字符串到字符串,从迭代到迭代)提供静态反射。



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

      #include <magic_enum.hpp>
      
      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>(color_name)
      if (color.has_value()) {
        // color.value() -> Color::GREEN
      };
      

      有关更多示例,请查看主存储库 https://github.com/Neargye/magic_enum

      For more examples check home repository https://github.com/Neargye/magic_enum.

      该库使用编译器特有的技巧(基于 __ PRETTY_FUNCTION __ / __ FUNCSIG __ ) ,适用于Clang> = 5,MSVC> = 15.3和GCC> = 9。

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

      枚举值必须在 [MAGIC_ENUM_RANGE_MIN,MAGIC_ENUM_RANGE_MAX ]


      • 默认情况下 MAGIC_ENUM_RANGE_MIN = -128 MAGIC_ENUM_RANGE_MAX = 128

      如果默认情况下所有枚举类型都需要另一个范围,重新定义宏 MAGIC_ENUM_RANGE_MIN MAGIC_ENUM_RANGE_MAX

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

      MAGIC_ENUM_RANGE_MIN 必须小于或等于 0 ,并且必须大于 INT16_MIN

      MAGIC_ENUM_RANGE_MIN must be less or equals than 0 and must be greater than INT16_MIN.

      MAGIC_ENUM_RANGE_ MAX 必须大于 0 ,并且必须小于 INT16_MAX

      MAGIC_ENUM_RANGE_MAX must be greater than 0 and must be less than INT16_MAX.

      如果需要其他范围用于特定的枚举类型,请为所需的枚举类型添加特殊化enum_range。

      If need another range for specific enum type, add specialization enum_range for necessary enum type.

      #include <magic_enum.hpp>
      
      enum number { one = 100, two = 200, three = 300 };
      
      namespace magic_enum {
      template <>
        struct enum_range<number> {
          static constexpr int min = 100;
          static constexpr int max = 300;
      };
      }
      


      这篇关于在现代C ++ 11 / C ++ 14 / C ++ 17和未来的C ++ 20中枚举为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆