如何将后续位分配给C枚举成员?
这是我得到的:
enum X {
NONE = 0x00000000,
FLAG_1 = 0x00000001,
FLAG_2 = 0x00000002,
FLAG_3 = 0x00000004,
FLAG_4 = 0x00000008,
FLAG_5 = 0x00000010,
// ...
FLAG_32 = 0x80000000
}
有没有一种方法可以使“位编号”自动变速,以便我想插入一个标志,以便下一步得到“重新编号”?
我只是在设计一个API,我想将相关标志放在一起,以特定的顺序进行订购。问题是,当我添加中间的内容时,我必须手动重新分配所有插入项目后进行的所有编号。假设在我的示例中,我想添加flag_2a = 0x00000004,而flag_3应为0x00000008,依此类推。是否有“完整的汽车”做到这一点?
好的,这是想到的第一件事:
#include <stdio.h>
enum { __FLAGS1_BASE = __COUNTER__ };
#define __FLAGS1_CT 1 << (__COUNTER__ - __FLAGS1_BASE - 1)
typedef enum __TEST1 {
FLAG1_0 = 0,
FLAG1_1 = __FLAGS1_CT,
FLAG1_2 = __FLAGS1_CT,
FLAG1_3 = __FLAGS1_CT,
FLAG1_4 = __FLAGS1_CT
} TEST1;
enum { __FLAGS2_BASE = __COUNTER__ };
#define __FLAGS2_CT 1 << (__COUNTER__ - __FLAGS2_BASE - 1)
typedef enum __TEST2 {
FLAG2_0 = 0,
FLAG2_1 = __FLAGS2_CT,
FLAG2_2 = __FLAGS2_CT,
FLAG2_3 = __FLAGS2_CT,
FLAG2_4 = __FLAGS2_CT
} TEST2;
int main() {
printf("X = %u\n", FLAG2_3); // should output 4.
return 0;
}
这是唯一的方法,还是比这简单的事情?
Here's what I got:
enum X {
NONE = 0x00000000,
FLAG_1 = 0x00000001,
FLAG_2 = 0x00000002,
FLAG_3 = 0x00000004,
FLAG_4 = 0x00000008,
FLAG_5 = 0x00000010,
// ...
FLAG_32 = 0x80000000
}
Is there a way to make "bit numbering" automatic so I could like insert a flag so all that goes next get "renumbered"?
I'm just designing an API and I want to keep related flags together, ordered in a specific sequence. The problem is when I add something that goes in the middle I have to manually reassign all numbering that goes after the inserted item. Let's say in my example I want to add FLAG_2A = 0x00000004, and FLAG_3 should be 0x00000008 and so on. Is there a "full auto" way of doing it?
OK, here's the first thing that comes to mind:
#include <stdio.h>
enum { __FLAGS1_BASE = __COUNTER__ };
#define __FLAGS1_CT 1 << (__COUNTER__ - __FLAGS1_BASE - 1)
typedef enum __TEST1 {
FLAG1_0 = 0,
FLAG1_1 = __FLAGS1_CT,
FLAG1_2 = __FLAGS1_CT,
FLAG1_3 = __FLAGS1_CT,
FLAG1_4 = __FLAGS1_CT
} TEST1;
enum { __FLAGS2_BASE = __COUNTER__ };
#define __FLAGS2_CT 1 << (__COUNTER__ - __FLAGS2_BASE - 1)
typedef enum __TEST2 {
FLAG2_0 = 0,
FLAG2_1 = __FLAGS2_CT,
FLAG2_2 = __FLAGS2_CT,
FLAG2_3 = __FLAGS2_CT,
FLAG2_4 = __FLAGS2_CT
} TEST2;
int main() {
printf("X = %u\n", FLAG2_3); // should output 4.
return 0;
}
Is it the only way, or is there something simpler than that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我将
make_flags
宏观HolyblackCat提出的快速实现汇总了:I threw together a quick implementation of the
MAKE_FLAGS
macro HolyBlackCat suggested:与 @chqrlie的答案(使用第二个枚举生成连续索引)相同,但使用宏生成:
然后
make_flags(e,none,(x)(y)(z))
展开至:Same idea as @chqrlie's answer (using a second enum to generate sequental indices), but generated with a macro:
Then
MAKE_FLAGS( E, none, (x)(y)(z) )
expands to:这是一种替代方法:
自动计算实际的位数和位标记。
Here is an alternative approach:
Actual bit numbers and bit marks are computed automatically.
最好的解决方案是手动输入它,仅32行。
如果您坚持使用宏来生成它,那么最少的糟糕版本不是 与一些“聪明”人为的人为运行的版本,但要使用“ x-macros”的事实上的标准解决方案, :
以上的前处理器输出:
The best solution is to type it out manually, it's just 32 rows.
In case you insist on generating it with macros, then the least bad version is not to run amok with some "clever" contrived ones, but to go with a de facto standard solution using "x-macros":
Pre-processor output of the above:
我的最后一个拍摄:flags.h头:
这样使用:
My last take: flags.h header:
Used like this:
有多少个组合?我是一个冗长的人,我会这样:
How many combinations are there? I am a verbose guy, I would do: