我相信这是 clang++ 中的一个错误,与访问类的公共成员函数有关

I believe this is a bug in clang++ related to the access to a class's public member function

本文关键字:访问 函数 成员 一个 clang++ 我相信 错误      更新时间:2023-10-16

以下内容不能在clang中编译:

#include <iostream>
void f() { std::cout << "f()n"; }
struct S {
    typedef void(*p)();
    operator p() { return f; }
};
int main()
{
    S s;
    s.operator p()();
}

收益 率:

main.cpp:13:16: error: unknown type name 'p'; did you mean 'S::p'?
    s.operator p()();
               ^
               S::p
main.cpp:6:19: note: 'S::p' declared here
    typedef void(*p)();
                  ^

但它应该,因为表达式s.operator p()()访问对象的公共成员函数S::s。我错过了什么吗?

如果我错了,我将不胜感激标准中支持答案的引用。

这似乎是 Clang 中的一个错误。 我相信代码是正确的。

Clang 4.0.0 报告:

<source>:13:16: error: unknown type name 'p'; did you mean 'S::p'?
    s.operator p()();
           ^

但是,从 C++14 3.4.5/7 [basic.lookup.classref]

如果 id 表达式是转换函数 id,则首先在 使用对象表达式和名称(如果找到(。否则,它将在整个上下文中查找 后缀表达式。在每个查找中,仅表示其专用化的类型或模板的名称 是考虑的类型。

[ 示例:

struct A { };
namespace N {
struct A {
    void g() { }
    template <class T> operator T();
};
}

int main() {
    N::A a;
    a.operator A();
       // calls N::A::operator N::A
}

— 结束示例 ]

在您的示例中,类型p应该在类中找到,而无需限定。