一个例子通常比长期解释更好.
您可以在Coliru上编译并运行此代码段.
(另一个前例子也可用)
#include <map>
#include <iostream>
struct MyClass
{
enum class …Run Code Online (Sandbox Code Playgroud) 假设我们有一些命名的枚举:
enum MyEnum {
FOO,
BAR = 0x50
};
Run Code Online (Sandbox Code Playgroud)
我搜索的是一个脚本(任何语言),它扫描我项目中的所有标题,并生成一个标题,每个枚举一个函数.
char* enum_to_string(MyEnum t);
Run Code Online (Sandbox Code Playgroud)
以及类似这样的实现:
char* enum_to_string(MyEnum t){
switch(t){
case FOO:
return "FOO";
case BAR:
return "BAR";
default:
return "INVALID ENUM";
}
}
Run Code Online (Sandbox Code Playgroud)
这个问题确实与typedefed枚举和未命名的C风格枚举有关.有人知道这个吗?
编辑:解决方案不应该修改我的源,除了生成的函数.枚举是在API中,因此使用迄今为止提出的解决方案不是一种选择.
我在一些库头文件中有一堆枚举类型,我正在使用,我想有一种方法将枚举值转换为用户字符串 - 反之亦然.
RTTI不会为我做这件事,因为'用户字符串'需要比枚举更具可读性.
一个强力解决方案将是一堆像这样的功能,但我觉得这有点像C样.
enum MyEnum {VAL1, VAL2,VAL3};
String getStringFromEnum(MyEnum e)
{
switch e
{
case VAL1: return "Value 1";
case VAL2: return "Value 2";
case VAL1: return "Value 3";
default: throw Exception("Bad MyEnum");
}
}
Run Code Online (Sandbox Code Playgroud)
我有一种直觉,认为使用模板有一个优雅的解决方案,但我还不能完全理解它.
更新:感谢您的建议 - 我应该明确说明枚举是在第三方库头中定义的,所以我不想更改它们的定义.
我现在的直觉是避免使用模板并执行以下操作:
char * MyGetValue(int v, char *tmp); // implementation is trivial
#define ENUM_MAP(type, strings) char * getStringValue(const type &T) \
{ \
return MyGetValue((int)T, strings); \
}
; enum eee {AA,BB,CC}; - exists in library header file
; …Run Code Online (Sandbox Code Playgroud) 这是我想要做的:
typedef enum { ONE, TWO, THREE } Numbers;
Run Code Online (Sandbox Code Playgroud)
我正在尝试编写一个函数来执行类似于以下的切换案例:
char num_str[10];
int process_numbers_str(Numbers num) {
switch(num) {
case ONE:
case TWO:
case THREE:
{
strcpy(num_str, num); //some way to get the symbolic constant name in here?
} break;
default:
return 0; //no match
return 1;
}
Run Code Online (Sandbox Code Playgroud)
而不是在每种情况下定义,有没有办法使用枚举变量设置它,就像我在上面尝试做的那样?
考虑以下代码:
#define F(x, ...) X = x and VA_ARGS = __VA_ARGS__
#define G(...) F(__VA_ARGS__)
F(1, 2, 3)
G(1, 2, 3)
Run Code Online (Sandbox Code Playgroud)
X = 1 and VA_ARGS = 2, 3两个宏的预期输出,这就是我用GCC得到的,但是,MSVC将其扩展为:
X = 1 and VA_ARGS = 2, 3
X = 1, 2, 3 and VA_ARGS =
Run Code Online (Sandbox Code Playgroud)
也就是说,__VA_ARGS__将其扩展为单个参数,而不是分解为多个参数.
有什么方法吗?
我意识到这已经在SO上不止一次被问过了,但我找不到一个问题明确地在C++ 11中寻找这个问题的当前解决方案,所以我们再来一次..
我们可以使用C++ 11方便地获取枚举的字符串值吗?
即(现在)C++ 11中的任何内置功能,它允许我们获取枚举类型的字符串表示,如
typedef enum {Linux, Apple, Windows} OS_type;
OS_type myOS = Linux;
cout << myOS
Run Code Online (Sandbox Code Playgroud)
那将Linux在控制台上打印?
在C++中有一种简单的方法可以将字符串转换为枚举(类似于Enum.ParseC#)吗?switch语句会很长,所以我想知道是否有更简单的方法可以做到这一点?
编辑:
感谢您的所有回复.我意识到,对于我的特定情况,有一种更简单的方法.字符串总是包含字符'S'后跟一些数字,所以我只是做了
int i = atoi(myStr.c_str() + 1);
Run Code Online (Sandbox Code Playgroud)
然后切换i.
将错误代码从枚举映射到字符串的更有效方法是什么?(在C++中)
例如,现在我正在做这样的事情:
std::string ErrorCodeToString(enum errorCode)
{
switch (errorCode)
{
case ERROR_ONE: return "ERROR_ONE";
case ERROR_TWO: return "ERROR_TWO";
...
default:
break;
}
return "UNKNOWN";
}
Run Code Online (Sandbox Code Playgroud)
如果我做这样的事情会以任何方式提高效率吗?:
#define ToStr( name ) # name;
std::string MapError(enum errorCode)
{
switch (errorCode)
{
case ERROR_ONE: return ToStr(ERROR_ONE);
case ERROR_TWO: return ToStr(ERROR_TWO);
...
default:
break;
}
return "UNKNOWN";
}
Run Code Online (Sandbox Code Playgroud)
也许有人对此有任何建议或想法?谢谢.
我想转换char *为enum所以我使用了这个.但转换char *到时我收到错误enum
我不能改变我的enum.也是type[]动态的简化我显示静态值.
enum type_t {
MSG_1 = 0,
MSG_2
};
char type[] = "MSG_1|MSG_2";
char *type_char;
type_char = strtok (type,"|");
while (type_char != NULL)
{
type_t type_enum = static_cast<type_t >(type_char );
type_char = strtok (NULL, "|;");
}
Run Code Online (Sandbox Code Playgroud)
我收到了以下错误
error: invalid static_cast from type 'char*' to type 'type_t'
Run Code Online (Sandbox Code Playgroud)
我想转换char *为enum