一个例子往往比一个长的解释要好.
您可以在 Coliru 上编译并运行此代码段.
(另一个前面的例子也是可用的)
#include
#define
开销.enum
->string
映射.enum
值从非零的数字开始enum
值enum
值类枚举
(C++11)class enum :
具有任何允许的
(C++11)std::map
不是一个好主意......)constexpr
(C++11,然后在 C++14/17/20 中放松)noexcept
(C++11)一个可能的想法是使用 C++ 编译器功能在编译时使用基于 variadic template class
和 constexpr
函数的元编程技巧生成 C++ 代码..
#include 枚举颜色 { 红色 = 2, 蓝色 = 4, 绿色 = 8 };颜色颜色 = 颜色::红色;auto color_name = magic_enum::enum_name(color);//颜色名称 ->红色的"std::string color_name{"GREEN"};自动颜色 = magic_enum::enum_cast(color_name)如果(颜色.has_value()){//color.value() ->颜色::绿色};
有关更多示例,请查看主存储库 https://github.com/Neargye/magic_enum.>
此库使用特定于编译器的 hack(基于 __PRETTY_FUNCTION__
/__FUNCSIG__
),适用于 Clang >= 5、MSVC >= 15.3 和 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
.
MAGIC_ENUM_RANGE_MIN
必须小于或等于 0
,并且必须大于 INT16_MIN
.
MAGIC_ENUM_RANGE_MAX
必须大于 0
且必须小于 INT16_MAX
.
如果需要特定枚举类型的另一个范围,请为必要的枚举类型添加特殊化 enum_range.
#include 枚举数 { 一 = 100, 二 = 200, 三 = 300 };命名空间magic_enum {模板 <>struct enum_range{静态 constexpr int min = 100;静态 constexpr int 最大值 = 300;};}
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) <<'
';
std::cout << magic(MyClass::MyEnum::BBB) <<'
';
std::cout << magic(MyClass::MyEnum::CCC) <<'
';
}
#define
overhead as minimum as possible.enum
-> string
mapping.enum
values starting from a number different from zeroenum
valuesenum
valuesclass enum
(C++11)class enum : <type>
having any allowed <type>
(C++11)std::map
is not a great idea...)constexpr
(C++11, then relaxed in C++14/17/20)noexcept
(C++11)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...
#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
};
For more examples check home repository https://github.com/Neargye/magic_enum.
This library uses a compiler-specific hack (based on __PRETTY_FUNCTION__
/ __FUNCSIG__
), which works on Clang >= 5, MSVC >= 15.3 and GCC >= 9.
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
.
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
must be less or equals than 0
and must be greater than INT16_MIN
.
MAGIC_ENUM_RANGE_MAX
must be greater than 0
and must be less than INT16_MAX
.
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 中枚举到字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!