抱歉,标题不清楚,如果您找到更好的标题,请随时进行编辑。相关主题已在Priority between normal function and Template function中进行了深入讨论。 , 但我没有找到问题的答案。
我的代码是:
template<typename T>
void f(T t){std::cout << "Template 1" << std::endl;} // template 1
template<typename T, typename B>
void f(T t){std::cout << "Template 2" << std::endl;} // template 2
int main () {
f(1); // line 1, template 1 will be called
f<int>(1); // template 1 will be called
f<int,int>(1); // template 2 will be called
}
第1行调用template 1函数的可能原因是什么?规范中是否明确定义?
在第 1 行,我认为编译器应该给出“模糊重载”错误。
最佳答案
B
无法推导(没有参数的类型为 B
),因此模板 1 是唯一可能的重载。
关于c++ - 具有相同名称的不同功能模板之间的重载优先级,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27419918/