【发布时间】:2019-02-23 13:52:54
【问题描述】:
我做了一些测试,遇到了这种奇怪的行为。
struct A{};
struct B : A{};
#include <iostream>
template<class T>
void fn2(T const &){
}
void fn2(A const &){
std::cout << "Here\n";
}
template<class T>
void fn1(){
T a;
fn2(a);
}
int main(){
fn1<B>();
}
我确实清理了代码。运行时,我希望它打印“here”。但是它调用fn2() 的模板版本。
我也在 Godbolt 中进行了测试。在那里我重写了函数fn1() 和fn2(),所以它们返回int。那时,编译器做了正确的事情。
这是我的编译方式:
$ gcc -Wall -Wextra -Wpedantic bug.cc -lstdc++
$ ./a.out
$ clang -Wall -Wextra -Wpedantic bug.cc -lstdc++
$ ./a.out
$
【问题讨论】:
-
什么意思?程序从不打印“Here”,
void和int都不会。
标签: c++ templates gcc clang overload-resolution