c++ - 具有相同名称的不同功能模板之间的重载优先级

标签 c++ templates overloading

抱歉,标题不清楚,如果您找到更好的标题,请随时进行编辑。相关主题已在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/

相关文章:

c++ - 如何在 C++ 中禁用转义序列

c++ - Stroustrup 的 Can_Copy 模板如何工作?

c++ - 模板参数列表中的第一个参数如何跳过表单调用参数?

C++:使用委托(delegate)构造函数时选择 `const char *` 与 `std::string`

c++ - 传递一个类的指针

c++ - 段错误取决于我在 C++ 中声明的顺序

c++ - Qt : How can I make a layout section stop expanding once the widgets its hosting stops expanding too

c++ - 推导出指向成员函数模板参数的指针的参数类型

c++ - 按返回类型重载

C# - 我怎样才能 "overload"委托(delegate)?