简体   繁体   English

嵌套名称说明符的名称查找规则

[英]Name lookup rules for nested-name-specifier

I've read the following (3.4.3/1): 我已阅读以下内容(3.4.3 / 1):

If a :: scope resolution operator in a nested-name-specifier is not preceded by a decltype-specifier, lookup of the name preceding that :: considers only namespaces, types, and templates whose specializations are types. 如果在嵌套名称说明符中的::作用域解析运算符之前没有decltype-specifier,则在:::之前的名称查找仅考虑专门针对类型的名称空间,类型和模板。

What is the lookup rule for nested-name-specifier? 嵌套名称说明符的查找规则是什么?

For example: 例如:

#include <iostrem>

namespace A
{
    int j=5;
}

int main()
{
    std::cout << A::j //I assume that A will be searched as just *unqualified-name* appeared inside the function which is a member of namespace
}

The second example: 第二个例子:

namespace A
{
    namespace B
    {
        int j=5;
    }
}

int main()
{
    std::cout << A::B::j
}

Is it true that in the second example A::B will be looking as qualified name inside the namespace? 在第二个示例中,A :: B是否将在名称空间中看起来是合格名称,这是真的吗? Ie we can define rules for nested-name-specifier lookup inductively. 也就是说,我们可以归纳定义嵌套名称说明符查找的规则。 But I cant find anything like that in the standard. 但我在标准中找不到类似的内容。 Is it true at all? 真的吗?

Yes, it's inductive, and I'd say it simply follows from the wording. 是的,它是归纳性的,我想说的只是从措辞上得出的。 First, let's add full parenthesizing based on the associativity of :: : 首先,让我们基于::的关联性添加完整的括号:

(std::cout) << ((A::B)::j)

(The above is just to demonstrate how the parser understands precedence, it's not valid code). (以上内容只是为了演示解析器如何理解优先级,它不是有效的代码)。

So j is qualified by the name A::B . 因此j由名称A::B限定。 It's a qualified name, so it's looked up according to 3.4.3. 这是一个合格的名称,因此根据3.4.3进行了查找。

A::B is itself a qualified name (it conforms to the syntactic form outlined by 5.1.1/8), so it is looked up according to rules for a qualified name. A::B本身是一个限定名称(符合5.1.1 / 8概述的语法形式),因此会根据规则查找限定名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM