类成员限定名称查找 [英] Class member qualified name lookup

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

问题描述

请考虑以下代码段:

  class A 
{
int b [A ::一个]; // 1,error
void foo(){int b = A :: a; } // 2,ok
static const int a = 5;
}

条款3.4.3.1/1(合格的名称查询,类成员)


如果限定id的嵌套名称说明符指定类,则在嵌套名称后面指定的
名称-specifier在(10.2)类的
范围中查找


// 1 中的嵌套名称说明符之后的名称 a //



条款10.2(成员名称查找)说:



10.2 / 2


以下步骤定义名称查找结果类范围C中的成员名称
f。


10.2 / 3


C中的f的查找集合,称为S(f,C)...



S(f,C)计算如下:


b
$ b


如果C包含名称f的声明,则声明集
包含在C中声明的满足
要求的每个声明


以下是我不清楚的地方: >

我引用的引号意味着 // 1 // 2 应用相同的成员查找规则。 :为什么我的推理错误了?



我知道关于不合格的名称查找规则到类范围。我理解了以下代码片段中的行为:

  class A 
{
int b [a ]; // error
void foo(){int b = a; } // ok
static const int a = 5;
}

这是因为3.4.1 / 7和3.4错误的原因是当 int b [A(a)] :: a]; 正在处理, A 尚未有符号 a 。在这个编译的时候, A 仍然不完整,因为我们还没有到达类定义的结束} 。编译器不前瞻,看看源代码的未来行是否包含 a

c

的定义。



您可以通过颠倒行的顺序看到这一点:

 类A 
{
static const int a = 5;
int b [A :: a]; // OK
};

函数定义没有同样的问题,因为内联函数体直到编译类定义。 (对不起,我没有这方面的标准参考资料)


Consider the following code snippet:

class A
{
    int b[A::a]; //1, error
    void foo(){ int b = A::a; } //2, ok
    static const int a = 5;
}

Clause 3.4.3.1/1 (Qualified name lookup, class members) said:

If the nested-name-specifier of a qualified-id nominates a class, the name specified after the nested-name-specifier is looked up in the scope of the class (10.2)

This implies that the name a after the nested-name-specifier both in //1 and in //2 will be looked up in the class scope.

Clause 10.2 (Member name lookup) said:

10.2/2

The following steps define the result of name lookup for a member name f in a class scope C.

10.2/3

The lookup set for f in C, called S(f, C)...

S(f, C) is calculated as follows:

10.2/4

If C contains a declaration of the name f, the declaration set contains every declaration of f declared in C that satisfies the requirements of the language construct in which the lookup occurs.

The following is unclear for me:

From the quotes I cited implies that for both //1 and //2 the same member lookup rules shall be applied. But actually its a different. Why is my reasoning wrong?

Note: I know about unqualified name lookup rules into the class scope. And I understood that behavior in the following code snippet:

class A
{
    int b[a]; //error
    void foo(){ int b = a; } //ok
    static const int a = 5;
}

It is because that behavior described in the sections 3.4.1/7 and 3.4.1/8 (Unqualified name lookup).

解决方案

The error is because when int b[A::a]; is being processed, A does not yet have a symbol a. At that point of compilation, A is still incomplete because we have not reached the closing } of the class definition yet. The compiler doesn't "look ahead" to see if future lines of source code contain a definition of a.

You can see this by reversing the order of the lines:

class A
{
    static const int a = 5;
    int b[A::a]; // OK
};

The function definition does not have the same problem because inline function bodies are not compiled until after compilation of the class definition. (Sorry, I don't have standard references handy for this)

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

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