【发布时间】:2018-06-19 07:50:41
【问题描述】:
template <class T>
void foo(T t) {
t.moo();
}
template <class T>
void f(T t) {
foo(t);
}
struct C {
};
void foo(C) {}
void foo(int) {}
int main() {
C c;
f(c);
return 0;
}
上面的代码编译没有错误。如果行“C c;”替换为“int c;”,编译器将生成错误“no function 'moo' for type 'int'”或类似的东西(MSVC 将编译,但这是另一回事)。如果我们将 int 重载替换为模板特化,一切都会再次起作用。如果将函数“void foo(int)”移到模板代码上方,那也可以。怎么回事?
【问题讨论】:
标签: c++ templates overloading