问题描述
在图书馆中,在,有一个有趣的 #define
似乎创建了一个 typedef
和一个枚举
,名称相同:
#define FI_ENUM(x)typedef int x;枚举x
这是由预处理器扩展为如下代码:
typedef int FREE_IMAGE_FILTER;
枚举FREE_IMAGE_FILTER {
FILTER_BOX = 0,
FILTER_BICUBIC = 1,
[...]
这是做什么的?具有相同名称的 typedef
和枚举
甚至是合法的
并不是枚举
与 int
兼容?为什么FreeImage这样做?
结构,工会和枚举的名称存在于自己的命名空间中。这就是为什么你可以声明一个 struct
/ union
/ 枚举
变量与实际的 struct相同的名称
/ union
/ 枚举
。
它不是完整的枚举的名称
(例如对于枚举X
我的意思是必须与整数兼容的 X
),它是枚举中的 中的名称。 / p>
In the library FreeImagePlus, in FreeImage.h
, there is a funny #define
which seems to create a typedef
and an enum
with the same name:
#define FI_ENUM(x) typedef int x; enum x
This is expanded by the preprocessor to code like:
typedef int FREE_IMAGE_FILTER;
enum FREE_IMAGE_FILTER {
FILTER_BOX = 0,
FILTER_BICUBIC = 1,
[...]
What does this do? Is it even legal to have a typedef
and an enum
with the same name?And isn't an enum
compatible to int
anyway? Why does FreeImage do this?
Names of structures, unions and enumerations lives in their own namespace. That's why you can declare a struct
/union
/enum
variable with the same name as the actual struct
/union
/enum
.
And it's not the name of the complete enum
(e.g. for enum X
I mean the X
) that has to be compatible with an integer, it's the names inside the enumeration.
这篇关于Typedef和枚举名称相同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!