C形式定义中的命名空间

Som*_*ame 2 c namespaces language-lawyer

我正在阅读N1570标准,并且有一个问题需要理解名称空间定义的措辞.就这个:

1如果在翻译单元中的任何一点可以看到多个特定标识符的声明,则语法上下文消除了引用不同实体的用法的歧义.因此,各种标识符类别都有单独的名称空间,如下所示:

- 标签名称(通过标签声明和使用的语法消除歧义);

- 关键字struct,union或enum 的结构,联合和枚举的标记(通过遵循任何32来消除歧义);

- 结构或工会的成员; 每个结构或联合为其成员都有一个单独的名称空间(通过用于通过.->运算符访问成员的表达式的类型消除歧义);

- 所有其他标识符,称为普通标识符(在普通声明符中声明或作为枚举常量).

32)标签只有一个名称空间,即使有三个可能.

在这里,他们正在谈论如果有超过1个特定标识符的声明是可见的.现在,类似"要访问标识符,应指定其名称空间"或"访问特定名称空间中的标识符......".

Sou*_*osh 7

首先,让我展示一个例子(这是严格的理解的目的,不写这样的代码,永远)

#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;
}
Run Code Online (Sandbox Code Playgroud)

你看到了标识符的用法here.它们根据规则属于单独的命名空间,因此该程序很好.

但是,例如,您更改结构变量名称,从而there更改here,您将看到冲突,因为在同一名称空间中将存在两个相同标识符(普通标识符)的单独声明.