具有相同名称的不同函数模板之间的重载优先级
Overloading priority between different function templates with identical name
很抱歉标题不清楚,如果您找到更好的标题,请随时对其进行编辑。在普通函数和模板函数之间的优先级中已经深入讨论了一个相关主题, 但我没有找到我问题的答案。
我的代码是:
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 行调用模板 1 函数的可能原因是什么?规范中是否明确定义?
在第 1 行,我认为编译器应该给出一个"模棱两可的重载"错误。
无法
推断B
(没有参数的类型为B
),因此模板 1 是唯一可能的重载。
相关文章:
- 当函数模板参数是具有默认参数的类模板时,函数模板参数的推导如何执行
- 将重载的成员函数传递给函数模板
- C++17中函数模板中的静态数组初始化(MSVC 2019)
- 为什么 gcc 和 clang 为函数模板的实例化生成不同的符号名称?
- 具有常量引用参数的函数模板专用化
- std::span<const T> 作为函数模板中的参数
- 如何编写一个完美的缩写函数模板?
- 仅在函数模板中为那些定义了函数的类型执行函数
- 如何在C++中伪造虚拟可变参数函数模板?
- 以下代码中的函数模板有什么问题?
- 在 C++20 中是否不再允许在 std 中对程序定义类型的函数模板进行专用化?
- 将显式实例化的函数模板与转换匹配
- 使用定义函数模板别名
- 函数模板返回类型
- C++有什么方法可以在既不调用函数模板也不提供其模板参数的情况下引用函数模板?
- C++ std::functional 中的可变参数函数模板
- 单行函数模板 c++ 的内联性保证
- 具有相同名称的不同函数模板之间的重载优先级
- 函数模板优先级大小写
- 如何使函数模板在ADL期间的优先级最低