【问题标题】:Member name lookup rules成员名称查找规则
【发布时间】:2014-06-18 19:50:24
【问题描述】:

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

10.2/3:

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

10.2/4:

如果 C 包含名为 f 的声明,则声明集 包含在 C 中声明的满足 f 的每个声明 查找发生的语言结构的要求。

考虑以下两个例子:

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)计算规则是什么,为什么?

【问题讨论】:

    标签: c++ class language-lawyer


    【解决方案1】:

    对于这些代码sn-ps

    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 Name lookup 部分中描述的名称查找。它们与您引用的引文没有共同之处。虽然我可以在第一类定义中展示名称 A::a 的 S(f, C) 是什么。所以 S(a, A) 仅来自一个声明 static const int a = 5

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

    另一条规则用于成员函数中的名称查找。在第一个类定义名称中会找到 A::a。

    正如我所指出的,所有这些都在标准的第 3.4 节中进行了描述。

    至于您引用的短语,那么更合适的示例将是例如以下

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

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

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

    void f( char );
    

    【讨论】:

    • 我不同意They have nothing common with the quotes you cited.是因为3.4.3.1/1说:If the nested-name-specifier of a qualified-id nominates a class, the name specified after the nested-namespecifier is looked up in the scope of the class (10.2)。这意味着将在我的代码 sn-ps 中应用成员名称查找。我说的对吗?
    • @Dmitry Fucintv 这个案例考虑了类是完整类型的情况,即类已经定义。
    • 但是在 10.2 的东西中并没有说类类型的完整性。所以我假设这个规则适用于完整和不完整的类类型。
    • @Dmitry Fucintv 我同意你的观点,标准的这个地方是模棱两可的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-19
    • 2016-07-05
    • 2016-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多