【发布时间】:2015-05-03 22:28:09
【问题描述】:
与所有其他类似问题相反,这个问题是关于使用新的 C++ 功能。
- 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++?
- 2011c++How to convert an enum type variable to a string?
- 2011c++Enum to String C++
- 2011 c++ How to convert an enum type variable to a string?
- 2012cHow to convert enum names to string in c
- 2013cStringifying an conditionally compiled enum in C
看了很多答案,还是没找到:
示例
一个例子通常比一个冗长的解释更好。
您可以在 Coliru 上编译和运行这个 sn-p。
(Another former example 也可用)
#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';
}
约束
- 请不要重复other answers 或basic link。
- 请避免臃肿的基于宏的答案,或尽量减少
#define开销。 - 请不要手动
enum->string映射。
很高兴拥有
- 支持
enum值从非零的数字开始 - 支持负的
enum值 - 支持分片的
enum值 - 支持
class enum(C++11) - 支持
class enum : <type>具有任何允许的<type>(C++11) - 编译时(而非运行时)转换为字符串,
或者至少在运行时快速执行(例如std::map不是一个好主意...) -
constexpr(C++11,然后在 C++14/17/20 中放宽) -
noexcept(C++11) - C++17/C++20友好的sn-p
一个可能的想法是使用 C++ 编译器功能在编译时使用基于 variadic template class 和 constexpr 函数的元编程技巧生成 C++ 代码...
【问题讨论】:
-
(也许是主题)看看这个 Qt 相关的博客。 woboq.com/blog/reflection-in-cpp-and-qt-moc.html。描述使用 C++ 反射(提议的标准)替换 Qt 的 moc(元对象编译器)的可能性。
-
N4113:
std::enumerator::identifier_v<MyEnum, MyEnum::AAA> -
一切都必须用 C++ 解决吗?为字符串表示自动生成代码非常容易,只需几行代码。
-
“如果可能,请不要提供基于 C 宏的答案”好吧,除非您愿意等待 C++17,否则几乎没有任何可用的东西,而且它不是 /i> 将枚举声明为
DEC_ENUM(enumname, (a,b,c,(d,b),(e,42)))是不好的,除非您必须维护生成宏......而且恕我直言,将这种情况放入语言中只是另一种黑客行为,而不是更强大的模板/宏混合体。我们不应该将所有有用的宏用例添加到语言中,只是为了能够说宏不再有用。 -
@olibre 这个问题今天至少有两个可用的答案。 1. @ecatmur 关于 C++17 的好回答,我们不能每次在 C++17 讨论中有更新时都对其进行编辑。请参阅mailing list of the reflection study group。 2. 我对当前 C++ 的语法很好的回答,许多人在生产中使用它,但在内部使用
#define。您所要求的是一个可用的解决方案。今天正确的答案是完全“正确”的解决方案要等到以后才可用(即现在接受@ecatmur)。
标签: c c c++ c++ c++ c++ c++ c++ c++ c c c++ string enums c++17 c++20