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

Ale*_*der 10 c++ public language-lawyer clang++

以下内容不能在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()();
}
Run Code Online (Sandbox Code Playgroud)

产量:

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)();
                  ^
Run Code Online (Sandbox Code Playgroud)

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

如果我错了,我会很感激标准中引用的答案.

Vau*_*ato 17

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

Clang 4.0.0报告:

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

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

如果id-expression是conversion-function-id,则首先在对象表达式的类中查找其conversion-type-id,并使用名称(如果找到).否则,它将在整个postfix-expression的上下文中查找.在每个查找中,仅考虑表示其特化类型的类型或模板的名称.

[例如:

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
}
Run Code Online (Sandbox Code Playgroud)

- 结束例子]

在您的示例中,p应该在类中找到类型而不需要限定.