c - C 中标识符的命名空间

标签 c namespaces c99

来自 (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/

相关文章:

c - 为什么在 C99 中不允许隐式声明 get()?

c - UL 后缀与 uint32_t 转换

c - 如果我在 C 程序中用 #define 或 const 声明常量会出错

在 C 中复制数组并清除 char 数组中的垃圾

C - 错误条件的定时器逻辑

c++ - 访问命名空间中结构的私有(private)数据成员

c - C99 宏中的循环构造

c - 在C中动态初始化字符串数组

php - 如何在 Symfony 的 Controller 中使用 PHP 扩展的功能

c++ - MSXML DOM : Add namespace declaration to an existing node in a tree