名称查找澄清 [英] Name lookup Clarification

查看:132
本文介绍了名称查找澄清的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


$ 10.2 / 4-[注意:在
中查找一个详细说明类型说明符(3.4.4)
或基本说明符(第10条) ,对于
实例,忽略所有非类型
声明,同时在嵌套名称说明符(3.4.3)中查找名称

忽略函数,变量和
枚举器声明。

$10.2/4- "[ Note: Looking up a name in an elaborated-type-specifier (3.4.4) or base-specifier (Clause 10), for instance, ignores all nontype declarations, while looking up a name in a nested-name-specifier (3.4.3) ignores function, variable, and enumerator declarations."

我已经发现这个语句在描述关于名称查找的时候很混乱。

I have found this statement to be very confusing in this section while describing about name lookup.

void S(){}

struct S{
   S(){cout << 1;}
   void f(){}
   static const int x = 0;
}; 

int main(){ 
   struct S *p = new struct ::S;  // here ::S refers to type
   p->::S::f();

   S::x;  // base specifier, ignores the function declaration 'S'

   ::S(); // nested name specifier, ignores the struct declaration 'S'.
   delete p;
} 

我的问题:


  1. 我对规则的理解是否正确?

  1. Is my understanding of the rules correct?

为什么 :: S 在执行新处理的行上自动表示struct S ,而在最后一行 :: S 表示 S 全局命名空间。

Why ::S on the line doing new treated automatically to mean struct S, whereas in the last line ::S means the functions S in the global namespace.

这是否指向文档中的模糊性,还是我还要离开C ++标准文档?

Does this point to an ambiguity in the documentation, or is it yet another day for me to stay away from C++ Standard document?


推荐答案

Q1:我想是这样。

Q2:与C的兼容性。当您在C中声明一个 struct 时,标记名称就是标记名称。为了能够以独立的方式使用它,你需要一个 typedef 。在C ++中,你不需要typedef,这使得活更容易。但是C ++规则已经变得复杂,因为需要能够导入已经存在的C标题,该标题使用函数名重载标签名。规范的例子是Unix stat()函数,它使用 struct stat * 作为参数。

Q2: Compatibility with C. When you declare a struct in C, the tag name is just that, a tag name. To be able to use it in a standalone way, you need a typedef. In C++ you don't need the typedef, that makes live easier. But C++ rules have been complicated by the need to be able to import already existing C headers which "overloaded" the tag name with a function name. The canonical example of that is the Unix stat() function which uses a struct stat* as argument.

Q3:标准阅读通常是相当困难的...你需要知道没有地方可以修改你正在阅读的内容。很奇怪,人们知道如何做到这一点是语言律师...

Q3: Standard reading is usually quite difficult... you need to already know that there is no place elsewhere modifying what you are reading. It isn't strange that people knowing how to do that are language lawyer...

这篇关于名称查找澄清的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆