c++ - 限定名称的重载解析

标签 c++ language-lawyer name-lookup qualified-name unqualified-name

考虑这个函数调用:

foo::bar();

11.3.1.1.1,第 3 段 [over.call.func] ( N4778 ) 涵盖了这种情况:

In unqualified function calls, the name is not qualified by an -> or . operator and has the more general form of a primary-expression. The name is looked up in the context of the function call following the normal rules for name lookup in function calls...

在这里,foo::bar是一个不合格的名字,在它没有被 -> 限定的意义上或 . 。所以本段适用。现在,短语“looked up in the context of”的含义在 6.4 的第 2 段中解释 [basic.lookup]:

A name “looked up in the context of an expression” is looked up as an unqualified name in the scope where the expression is found.

然而,foo::bar在名称查找领域 中的限定名称。也就是说,这个段落组合基本上就是说,限定名foo::bar由非限定名称查找规则查找。但是,我不认为非限定名称查找能够递归进入更窄的范围,即 foo。至 bar .这是缺陷吗?

最佳答案

不,我不认为这是一个缺陷。它说

The name is looked up in the context of the function call following the normal rules for name lookup in function calls [...]

正如您从我突出显示的部分中看到的那样,该标准指定了应该如何查找名称:通过名称查找。

名称查找包括非限定、限定和参数相关查找,因此您的名称确实由限定名称查找规则解析。

“在 expr 的上下文中查找”规则在这里不适用,因为它指定了使用什么规则。该段只有在没有时才发挥作用。例如,在 [class.qual]p1 中:

the names in a template-argument of a template-id are looked up in the context in which the entire postfix-expression occurs.

关于c++ - 限定名称的重载解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52889777/

相关文章:

c++ - 为什么由 const 限定的变体成员组成的 union 会导致没有默认的默认构造函数?

c++ - 为什么命名空间内定义的 << 运算符不参与重载解析?

c++ - 在具有父 ID 的一般树中找到最低公共(public)祖先

c++ - Makefile 未链接必要的 .hpp 文件

c++ - 智能指针和 dynamic_cast

c++ - 将字符串字面量的地址映射到字符串字面量,通过解析ELF C++程序

c++ - 将 std::vector 附加到自身,未定义的行为?

c++ - [basic.execution] p5 句子 2 的意图

c++ - 具有 `stdio.h` 函数的模板和名称查找

c++ - 依赖限定名的查找