c – 在名称相同的不同功能模板之间重载优先级

前端之家收集整理的这篇文章主要介绍了c – 在名称相同的不同功能模板之间重载优先级前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
抱歉标题不清楚,如果找到更好的标题,请随时编辑.一个相关的主题已在 Priority between normal function and Template function深入讨论,
但我找不到我的问题的答案.

我的代码是:

  1. template<typename T>
  2. void f(T t){std::cout << "Template 1" << std::endl;} // template 1
  3.  
  4. template<typename T,typename B>
  5. void f(T t){std::cout << "Template 2" << std::endl;} // template 2
  6.  
  7. int main () {
  8. f(1); // line 1,template 1 will be called
  9. f<int>(1); // template 1 will be called
  10. f<int,int>(1); // template 2 will be called
  11. }

在第1行调用模板1函数的可能原因是什么?它在规范中是否定义明确?

在第1行,我认为编译器应该给出“模糊过载”错误.

解决方法

无法推导出B(没有参数具有类型B),因此模板1是唯一可能的超载.

猜你在找的C&C++相关文章