C/C++枚举和 char * 数组

发布于 2024-10-16 17:38:59 字数 369 浏览 2 评论 0原文

在一篇文章中遇到以下代码,并不认为它是 char* 数组的标准 C/C++ 语法。作为测试,Visual C++ (Visual Studio 2005) 和 C++ Builder Rad XE 都拒绝第二行。

如果不使用#define,任何人都有任何技巧/技巧可以保持枚举和字符串数组同步而不诉诸STL吗?

更多的是一个好奇的问题。

enum TCOLOR { RED, GREEN, BLUE };

char *TNCOLOR[] = { [RED]="Red", [GREEN]="Green", [BLUE]="Blue" };

顺便说一句,这篇文章来自相当旧的文章,我相信这可能在 GCC 下工作,但尚未测试。

Ran accross the following code in an article and didn't think it was standard C/C++ syntax for the char* array. As a test, both Visual C++ (visual studio 2005) and C++ Builder Rad XE both reject the 2nd line.

Without using #defines, anyone have any tricks/tips for keeping enums and a string array sort of in sync without resorting to STL ?

More of a curiosity question.

enum TCOLOR { RED, GREEN, BLUE };

char *TNCOLOR[] = { [RED]="Red", [GREEN]="Green", [BLUE]="Blue" };

as an aside, the article this came from is quite old and I believe this might work under GCC but have not tested.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

且行且努力 2024-10-23 17:38:59

这些是 C99 指定的初始值设定项。 GCC 在 C90 模式(和 C++)中支持它们作为扩展。在这里阅读:

http://gcc.gnu.org /onlinedocs/gcc/Designated-Inits.html#Designated-Inits

没有好的方法可以使枚举和字符串保持同步。如果我真的需要这个,那么我会编写一个脚本来从源代码中获取枚举声明并从中生成字符串数组。我真的很讨厌用宏来做这件事。

更新:这是去年的一个问题,讨论 enum->string 转换(在本例中用于打印)

C++:将枚举值打印为文本

These are C99 designated initializers. GCC supports them in C90 mode (and in C++) as an extension. Read about it here:

http://gcc.gnu.org/onlinedocs/gcc/Designated-Inits.html#Designated-Inits

There is no good way to keep enums und strings in sync. If I'd really need this, then I'd write a script to grab the enums declarations from the source code and generate the strings arrays from that. I really hate doing this with macros.

UPDATE: Here's a question from last year which discusses enum->string conversion (for printing in this case)

C++: Print out enum value as text

旧时光的容颜 2024-10-23 17:38:59
char *TNCOLOR[] = { [RED]="Red", [GREEN]="Green", [BLUE]="Blue" };

在 C99 中允许,在 C++03、C++0x 或任何其他 C 版本中不允许

阅读有关 聚合类型的指定初始值设定项 - C99

char *TNCOLOR[] = { [RED]="Red", [GREEN]="Green", [BLUE]="Blue" };

This is allowed only in C99, not in C++03, C++0x, or any other version of C.

Read about Designated initializers for aggregate types - C99.

影子的影子 2024-10-23 17:38:59

这是C99语法,GCC支持的语法。根据您的要求,

  • 没有 #define
  • 没有 STL,

您可能找不到同步。

This is C99 syntax, what is supported by GCC. With your requirements

  • no #define
  • no STL

you will probably not find a sync.

情深缘浅 2024-10-23 17:38:59

我意识到这是一个老问题,但这可能会帮助其他人寻找一种简单的方法来保持多个数组/枚举同步。

就我而言,我只是想进行编译时检查来确定我的列表是否不同步,并且由于在预处理器执行完操作之后才评估 sizeof 运算符,因此这是最简单的方法这样做。

在某种头文件中...

enum ColorType
{
    Red=0,
    Blue,
    Green,
    ColorType_Max
};

在同一个头文件或其他文件中...

char const* ColorTypeNames[]=
{
    "Red",
    "Blue",
    "Green"
};

在某个 cpp 文件中...

const int s_ColorTypeCount = (int)ColorType_Max;
const int s_ColorNameCount = (int)(sizeof(ColorTypeNames)/sizeof(char const*));
const int s_ColorErrorA = 1/(s_ColorTypeCount/s_ColorNameCount);
const int s_ColorErrorB = 1/(s_ColorNameCount/s_ColorTypeCount);

只有当两个大小匹配时,s_ColorErrorA 和 s_ColorErrorB 才会等于 1。由于变量是常量,当两个计数变量不同时,这将生成编译时除以零错误。

I realize this is an old question, but this might help anyone else looking for a simple way to keep multiple arrays/enums in sync.

In my case, I simply wanted a compile-time check to determine whether my lists were out of sync, and since the sizeof operator is not evaluated until after the preprocessor does it's thing, this was the simplest way to do that.

In a header file of some sort...

enum ColorType
{
    Red=0,
    Blue,
    Green,
    ColorType_Max
};

In the same header or some other file perhaps...

char const* ColorTypeNames[]=
{
    "Red",
    "Blue",
    "Green"
};

In a cpp file somewhere...

const int s_ColorTypeCount = (int)ColorType_Max;
const int s_ColorNameCount = (int)(sizeof(ColorTypeNames)/sizeof(char const*));
const int s_ColorErrorA = 1/(s_ColorTypeCount/s_ColorNameCount);
const int s_ColorErrorB = 1/(s_ColorNameCount/s_ColorTypeCount);

Only when the two sizes match, will s_ColorErrorA and s_ColorErrorB equal 1. Since the variables are constants, this will generate a compile-time divide by zero error when the two count variables differ.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文