在重构一个相当大的代码库的过程中,我的编译器想出了一个误解我的好方法.这是我所说的最简单的例子:
#include <iostream>
class Foo {
public:
virtual int get() = 0;
template <typename T> int get(int i) { return 4 + i; }
};
class Bar : public Foo {
public:
virtual int get() { return 3; }
};
int main(int argv, char **argc) {
Bar b;
std::cout << b.get<char>(7) << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
Clang 3.6,gcc 4.7,gcc 4.8和gcc 4.9都将"b.get(7)"标记为"b.get"和"char"之间的比较运算符.
template-test.cpp: In function ‘int main(int, char**)’:
template-test.cpp:16:17: error: invalid use of non-static member function
std::cout << b.get<char>(7) << std::endl; …Run Code Online (Sandbox Code Playgroud)