用#define重新定义枚举枚举数 [英] Redefining enum enumerators with #define
问题描述
我在 C 头文件中发现了某些内容,但我无法确定其用途。例如,在文件 bits / socket.h
中,存在枚举类型枚举__socket_type
,但是在每个枚举器之后都有一个定义宏的定义相同。示例:
I have spotted something in C header files what I can't figure out what is for. For example in file bits/socket.h
there is an enumeration type enum __socket_type
, but after every enumerator there is a define macro which defines the same. Example:
enum __socket_type
{
SOCK_STREAM = 1,
#define SOCK_STREAM SOCK_STREAM
...
};
我一直无法知道这是干什么的。请赐教。我什至不知道如何形成查询Google或此站点搜索框的正确问题。
I have been unable to find out what this is for. Please enlighten me. I don't even know how to form right question for querying google nor this site search box.
推荐答案
预处理器宏永远不会递归扩展,因此 #define
所做的就是将名称保留在任何使用位置。当您想进行预处理器功能测试时,这些事情很有用。
A prepreprocessor macro will never expand recursively, so what such a #define
does is leave the name in place whereever it is used. Such things are useful when you want to have a preprocessor feature test.
#ifdef SOCK_STREAM
..
#endif
之后可以用来有条件地编译一些代码。
can be used to conditionally compile some code afterwards.
编辑:因此,这将枚举(没有冲突和作用域的隐式值)的更干净方法与预处理器测试结合在一起。
So this combines the cleaner approach of enumerations (implicit values without collisions and scoping) with preprocessor tests.
这篇关于用#define重新定义枚举枚举数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!