c - C 形式定义中的命名空间

标签 c namespaces language-lawyer

我正在阅读 N1570标准并且在理解 namespace 定义的措辞方面有问题。这是它:

1 If more than one declaration of a particular identifier is visible at any point in a translation unit, the syntactic context disambiguates uses that refer to different entities. Thus, there are separate name spaces for various categories of identifiers, as follows:

— label names (disambiguated by the syntax of the label declaration and use);

— the tags of structures, unions, and enumerations (disambiguated by following any32) of the keywords struct, union, or enum);

— the members of structures or unions; each structure or union has a separate name space for its members (disambiguated by the type of the expression used to access the member via the . or -> operator);

— all other identifiers, called ordinary identifiers (declared in ordinary declarators or as enumeration constants).

32) There is only one name space for tags even though three are possible.

他们在这里谈论的是在超过 1 个特定标识符声明可见的情况下。现在,类似“要访问标识符,应指定其命名空间”或“要访问特定命名空间中的标识符...”之类的话。

最佳答案

让我先举个例子(这是严格为了理解的目的,不要写这样的代码,永远)

#include  <stdio.h>


int main(void)
{
    int here = 0;    //.......................ordinary identifier
    struct here {    //.......................structure tag
        int here;    //.......................member of a structure
    } there;

here:                             //......... a label name
     here++;
     printf("Inside here\n");
     there.here = here;           //...........no conflict, both are in separate namespace
     if (here > 2) {
         return 0; 
     }
     else
       goto here;                 //......... a label name

    printf("Hello, world!\n");          // control does not reach here..intentionally :)
    return 0;
}

您可以在此处看到标识符的用法。根据规则,它们属于不同的命名空间,因此这个程序没问题。

但是,例如,您将结构变量名称从 there 更改为 here,您会看到冲突,因为那时会有同一命名空间中同一标识符(普通标识符)的两个单独声明。

关于c - C 形式定义中的命名空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53442007/

相关文章:

c++ - 什么是 C 中 C++ 的新/删除等价物?

c - 如何以编程方式启动互联网连接?

javascript - 编译在线 html 表单上提交的代码并使用 gcc 处理它的过程

python - 未绑定(bind)的变量和名称

linux-kernel - debugfs 在网络命名空间中不可用

backbone.js - 主干命名空间约定最佳实践

c++ - 这些转换中的哪些应该是不明确的?

c++ - lambda 作为静态成员

c - 重复完全相同的代码段 1000 次而不循环

c++ - `std::function` 和之前推导的模板参数的替换失败 - 为什么?