【发布时间】:2015-01-20 21:28:33
【问题描述】:
我刚刚创建了一个非常小的项目,当我遇到一个我无法追踪的有趣的编译器错误时,我认为我可以立即完成(它是关于基本委托的)。这是代码的简化版本:
class NoComp {
};
class Comp {
bool operator==(const Comp& other)
{ std::cout << "Working!" << std::endl; return true;}
};
struct Test {
template<typename T>
Test(T&& obj) {}
bool operator==(const Test& other);
};
int main()
{
Test a(Comp());
Test b(NoComp());
a.operator ==(b);
}
使用g++ version 4.8.3 20140911 (Red Hat 4.8.3-7) (GCC) found here 编译时会产生以下编译器错误:
main.cpp: In function 'int main()':
main.cpp:22:13: error: request for member 'operator==' in 'a', which is
of non-class type 'Test(Comp (*)())'
a.operator ==(b);
我无法弄清楚该错误的含义以及它为什么存在。那里发生了什么,它是一个错误还是被标准覆盖?如果可以的话,我该如何躲避呢?
【问题讨论】:
-
最麻烦的解析...
-
是的 - 你在声明一个类型
-
...一个函数,而不是
-
有一个类似的问题:Compiler optimization or my misunderstanding。请注意,正如我在回答中所展示的那样,
clang会给您一个非常有用的警告,在多个编译器中尝试示例通常会有所帮助。
标签: c++ templates c++11 compiler-errors