我相信这是 clang++ 中的一个错误,与访问类的公共成员函数有关
I believe this is a bug in clang++ related to the access to a class's public member function
以下内容不能在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
应该在类中找到,而无需限定。
相关文章:
- 如何在函数外部访问函数中局部变量的值?
- UChildActorComponent的ChildActorClass中的访问函数
- 访问函数变体时"Invalid conversion"错误
- 在将向量作为参数传递给函数后,我无法访问函数中向量的元素
- 在64位DLL中访问函数的错误
- 访问函数中 const char *arr[] 的元素
- 访问函数的调用方类型
- 访问函数中的使用声明:与范围相关的问题
- 通过 -> 语法 c++ 从指针访问函数
- 在函数中使用 const 引用参数访问函数中成员的最佳实践 C++.
- 从另一个文件(C++,QT)访问函数未解析的外部
- 如何从主(C )访问函数中的值
- 访问函数 c++ 中的全局变量
- C++ GoogleTest 使用夹具 - 无法访问函数指针定义
- 在不知道名称的情况下访问函数参数
- C++:直接访问器或类内的访问函数
- 无法从对象访问函数
- 类的2D矢量在访问函数时出错
- 为什么在C++中访问函数时没有对象
- 我无法使用指针数组从main访问函数(在类中)