来自 (6.2.3/1) C99 标准
-- the tags of structures, unions, and enumerations (disambiguated by following any* of the keywords struct, union, or enum);
*footnote: There is only one name space for tags even though three are possible.
现在,我知道在 C++ 中我们使用命名空间来避免实体冲突,例如:
namespace foo {
struct bar // ..define bar..
};
namespace foo2 {
struct bar // ..define bar..
};
在 C 语言中,struct、union、enum 如何充当命名空间?脚注中的命名空间只有一个而不是三个是什么意思?
最佳答案
结构体、 union 体和枚举的标签有一个单独的命名空间,因此您可以拥有
struct foo {
int bar;
};
double foo;
因为标签的命名空间与标识符的命名空间是分开的。标签只有一个命名空间,这意味着您不能拥有
struct foo {
int bar;
};
union foo {
double baz;
long quux;
}
在同一范围内。
关于c - C 中标识符的命名空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13547915/