相关疑难解决方法(0)


有没有一种简单的方法将C++枚举转换为字符串?

假设我们有一些命名的枚举:

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中,因此使用迄今为止提出的解决方案不是一种选择.

c++ string scripting enums

118
推荐指数
12
解决办法
11万
查看次数

如何轻松地将c ++枚举映射到字符串

我在一些库头文件中有一堆枚举类型,我正在使用,我想有一种方法将枚举值转换为用户字符串 - 反之亦然.

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)

c++ enums templates

113
推荐指数
8
解决办法
20万
查看次数

在C中使用枚举类型的变量作为字符串的简单方法?

这是我想要做的:

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)

而不是在每种情况下定义,有没有办法使用枚举变量设置它,就像我在上面尝试做的那样?

c enums c-preprocessor

86
推荐指数
8
解决办法
9万
查看次数

如何在c中将枚举名称转换为字符串

是否有可能将枚举器名称转换为C中的字符串?

c string enums

82
推荐指数
5
解决办法
9万
查看次数

MSVC不会正确扩展__VA_ARGS__

考虑以下代码:

#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__将其扩展为单个参数,而不是分解为多个参数.

有什么方法吗?

visual-c++ c-preprocessor variadic-macros

48
推荐指数
2
解决办法
1万
查看次数

枚举到C++ 11中的字符串

我意识到这已经在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在控制台上打印?

enums c++11

29
推荐指数
3
解决办法
3万
查看次数

C++字符串到枚举

在C++中有一种简单的方法可以将字符串转换为枚举(类似于Enum.ParseC#)吗?switch语句会长,所以我想知道是否有更简单的方法可以做到这一点?

编辑:

感谢您的所有回复.我意识到,对于我的特定情况,有一种更简单的方法.字符串总是包含字符'S'后跟一些数字,所以我只是做了

int i = atoi(myStr.c_str() + 1);
Run Code Online (Sandbox Code Playgroud)

然后切换i.

c++ string enums

17
推荐指数
6
解决办法
7万
查看次数

在C++中将错误代码映射到字符串

将错误代码从枚举映射到字符串的更有效方法是什么?(在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)

也许有人对此有任何建议或想法?谢谢.

c++ string error-code

4
推荐指数
2
解决办法
5191
查看次数

在c ++中将char*转换为枚举

我想转换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

c c++ enums

1
推荐指数
1
解决办法
2511
查看次数