简体   繁体   English

如何解析C ++中的枚举?

[英]How can I parse an enum in C++?

I have a C++ enum of error codes (30 of them) and there is a function that returns an error code in case of a problem. 我有一个错误代码的C ++枚举(其中有30个),并且有一个函数在出现问题时返回错误代码。 Is there a way to parse the enum so as to check which of the error codes has been returned and provide the interpretation? 有没有一种方法可以解析枚举,以便检查返回了哪个错误代码并提供解释? I know switch statement could be an option here but was looking for something different to avoid writing lots of switch statements. 我知道在这里可以选择switch语句,但是我在寻找不同的东西来避免编写很多switch语句。

No, it is not possible: names of enum constants are compile-time artifacts, they are not available at runtime * . 不,这是不可能的:枚举常量的名称是编译时工件,它们在运行时不可用*

You can make a map<string,MyEnumType> and populate it with names of enums and their values. 您可以创建一个map<string,MyEnumType>并用枚举的名称及其值填充它。 You can use "stringize macros" to avoid typing the same value multiple times: 您可以使用“字符串化宏”来避免多次键入相同的值:

#include <iostream>
#include <string>
#include <map>
using namespace std;

#define ADD_ENUM_TO_MAP(m,x) m[#x]=x

enum MyEnumType {
    quick, brown, fox, jumps, over, the, lazy, dog
};

int main() {
    map<string,MyEnumType> nameToEnum;
    ADD_ENUM_TO_MAP(nameToEnum, quick);
    ADD_ENUM_TO_MAP(nameToEnum, brown);
    ADD_ENUM_TO_MAP(nameToEnum, fox);
    ADD_ENUM_TO_MAP(nameToEnum, jumps);
    ADD_ENUM_TO_MAP(nameToEnum, over);
    ADD_ENUM_TO_MAP(nameToEnum, the);
    ADD_ENUM_TO_MAP(nameToEnum, lazy);
    ADD_ENUM_TO_MAP(nameToEnum, dog);
    cout << nameToEnum["fox"] << endl;
    return 0;
}

Demo. 演示。

* Debuggers obtain this information through a symbol table provided by the compiler. *调试器通过编译器提供的符号表获取此信息。

What you can do is providing a map (as I mentioned in my comment): 您可以做的就是提供地图(正如我在评论中提到的那样):

enum ErrorCodes {
    OK ,
    ERR_FILE_OPEN ,
    ERR_MISSING_INPUT ,
    // ...
}

std::map<ErrorCodes,std::string> codeTranslationMap {
     { OK, "OK" }
     { ERR_FILE_OPEN , "File open failed." }
     { ERR_MISSING_INPUT , "Missing input." }
    // ...
};

In complement of dashblinkenlight's answer , you could play X-macro C&C++ preprocessor tricks like: 为了补充dashblinkenlight的答案 ,您可以玩X宏 C&C ++ 预处理器技巧,例如:

 // your list of enumerators applied to a preprocessor macro:
 #define ENUM_LIST(P) \
    P(ERR_NONE,"no errors") \
    P(ERR_FILE_OPEN,"file open") \
    P(MISSING_INPUT,"missing input")

then you would first define the enum using: 那么您将首先使用以下方法定义枚举:

 #define ENUM_DEFINE(Nam,Str) Nam,
 enum error_en {
    ENUM_LIST(ENUM_DEFINE)
 };
 #undef ENUM_DEFINE

and later define the printing eg with 然后定义打印,例如

 void out(enum error_en e) {
   switch(e) {
 #define ENUM_OUT(Nam,Str) case Nam: cout << Str; break;
 ENUM_LIST(ENUM_OUT)
 #undef ENUM_OUT
    } // end switch
 }

Or you could play similar tricks to provide the equivalent of πάντα ῥεῖ 's answer 或者,您可以玩类似的把戏,以提供πάνταῥεῖ的答案

See also this answer about creative uses of multiple #include -s of the same file. 另请参见关于同一文件的多个#include -s的创造性使用的此答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM