【问题标题】:Overloading priority between different function templates with identical name同名的不同功能模板之间的重载优先级
【发布时间】:2014-12-11 09:56:29
【问题描述】:

抱歉,标题不清楚,如果您找到更好的,请随时编辑。一个相关的话题已经在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 行调用模板 1 函数的可能原因是什么?规范中有明确定义吗?

在第 1 行,我认为编译器应该给出“模棱两可的重载”错误。

【问题讨论】:

    标签: c++ templates overloading


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多