我正在阅读 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/