成员名称查找规则 [英] Member name lookup rules

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

问题描述

秒。 10.2描述了成员名称查找规则:

The sec. 10.2 describes member name lookup rules:

10.2 / 3:


在C中设置为f,称为S(f,C),由两个组件
组成:声明集,一组成员名为f;和子对象
set,一组子对象,其中找到这些成员的声明(可能是
包括使用声明)。在声明集中,
使用声明被它们指定的成员替换,
类型声明(包括注入类名)被它们指定的
类型替换。 S(f,C)计算如下:

The lookup set for f in C, called S(f, C), consists of two component sets: the declaration set, a set of members named f; and the subobject set, a set of subobjects where declarations of these members (possibly including using-declarations) were found. In the declaration set, using-declarations are replaced by the members they designate, and type declarations (including injected-class-names) are replaced by the types they designate. S(f, C) is calculated as follows:

10.2 / 4:


如果C包含名称f的声明,则声明集
包含C中声明的每个声明,满足语言结构的
要求,其中

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.

请考虑以下两个示例:

class A
{
    void foo(){ A::a; } //S(a, A)={ static const int a; }
    static const int a = 5;
}

class A
{
    int b[A::a]; //S(a, A) is empty and the program is ill-formed
    static const int a = 5;
}



实际的S(f,C)计算规则是什么? / p>

What is the actual S(f, C) calculation rules and why?

推荐答案

对于这些代码片段

class A
{
    void foo(){ A::a; } //S(a, A)={ static const int a; }
    static const int a = 5;
};

class A
{
    int b[A::a]; //S(a, A) is empty and the program is ill-formed
    static const int a = 5;
};

您应该考虑部分中描述的名称查找3.4名称查找。他们与你引用的引语没有什么共同点。虽然我可以显示什么是S(f,C)例如名称A :: a在第一类定义。所以S(a,A)只能从一个声明中得到。 static const int a = 5

you should consider the name lookup that is described in section 3.4 Name lookup of the Standard. They have nothing common with the quotes you cited. Though I can show what is S(f, C) for example for name A::a in the first class definition. So S( a, A ) cosists only from one declaration static const int a = 5

在第二个类定义名称 A :: a 将不会被找到,因为它必须在其使用之前声明。

Take into account that in the second class definition name A::a will not be found because it has to be declared before its usage.

另一个规则用于成员函数中的名称查找。

The other rule is used for the name lookup in member functions. In the first class definition name A::a will be found.

所有这一切都在我指出的标准的3.4节中描述。​​

All this is described in section 3.4 of the Standard as I pointed out.

对于您引用的短语,更合适的例子将是例如以下

As for the phrase you cited then the more appropriate example will be for example the following

struct A
{
   void f( int );
};

struct B : A
{
   using f;
   void f( char );
};

在这种情况下,如果搜索名称f,那么S(f,B)将包含两个声明

In this case if the name f is searched then S( f, B ) will contain two declarations

using f; // or void f( int );

void f( char );

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

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