模板功能的重载解析 [英] Overload resolution of template functions

查看:94
本文介绍了模板功能的重载解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下代码:

#include <iostream>

//Number1
template<typename T1, typename T2>
auto max (T1 a, T2 b)
{
    std::cout << "auto max(T1 a, T2 b)" <<std::endl;
    return  b < a ? a : b;
}

//Number2
template<typename RT, typename T1, typename T2>
RT max (T1 a, T2 b)
{
    std::cout << "RT max(T1 a, T2 b)" << std::endl;
    return  b < a ? a : b;
}


int main()
{
    auto a = ::max(4, 7.2);         //Select Number1

    auto b = ::max<double>(4, 7.4); //Select Number2

    auto c = ::max<int>(7, 4.);     //Compile-time error overload ambiguous

    auto c = ::max<double>(7, 4.); //Select Number2

}

auto c = :: max< int>(7,4。); :该行由于过载歧义而无法编译,并显示以下消息:

auto c = ::max<int>(7, 4.); : This line fails to compile because of overload ambiguity with the following message:

maxdefault4.cpp:9:27: error: call of overloaded 'max(int, double)' is ambiguous
  auto c = ::max<int>(7, 4.);
                           ^
maxdefault4.cpp:9:27: note: candidates are:
In file included from maxdefault4.cpp:1:0:
maxdefault4.hpp:4:6: note: auto max(T1, T2) [with T1 = int; T2 = double]
 auto max (T1 a, T2 b)
      ^
maxdefault4.hpp:11:4: note: RT max(T1, T2) [with RT = int; T1 = int; T2 = double]
 RT max (T1 a, T2 b)
    ^

同时执行以下代码:
àutoc = :: max< double>(7,4)成功,为什么我们没有相同的错误消息说 max< double> 的调用是不明确的,就像 max< int> 失败的方式一样?

while the following code: àuto c = ::max<double>(7, 4.) succeed, why don't we have the same error message saying the call is amgiguous for max<double> the same the way max<int> failed ?

为什么 double 没问题?

我读过在《 C ++模板,完成指南》一书中,模板参数推导未考虑返回类型,因此为什么 max< int> 是模棱两可而不是 max< double>

I've read in "C++ templates, the completes Guide" book that the template argument deduction does not take the return type into account, so why max<int> is ambiguous and not max<double>?

自变量降级中是否确实未考虑模板函数的返回类型?

Does the return type of a template function really not taken into account in argument dedcution ?

推荐答案


模板自变量推导未考虑返回类型,

the template argument deduction does not take the return type into account,

是的。 模板参数推导是基于函数参数执行的。

Yes. Template argument deduction is performed based on function arguments.


为什么 max< int> 是模棱两可的,而不是 max< double> code>?

so why max<int> is ambiguous and not max<double>?

给出 :: max< int<(7,4。),对于第一个重载,将第一个模板参数 T1 指定为 int T2 从第二个函数参数 4推导出为 double ,那么实例化为 double max(int,double) 。对于第二次重载,将第一个模板参数 RT 指定为 int T1 7 T2 int c>从 4推导出为 double ,则实例化为 int max(int,double) 重载解析也不考虑返回类型,两个重载都是完全匹配,然后模糊。

Given ::max<int>(7, 4.), for the 1st overload, the 1st template parameter T1 is specified as int, and T2 is deduced as double from the 2nd function argument 4., then the instantiation would be double max(int, double). For the 2nd overload, the 1st template parameter RT is specified as int, T1 is deduced as int from 7, T2 is deduced as double from 4., then the instantiation would be int max(int, double). Overload resolution doesn't consider return type too, the two overloads are both exact match and then ambiguous.

给出 :: max< double>(7,4。),对于第一个重载,将第一个模板参数 T1 指定为 double T2 4推导为 double ,因此实例化为 double max(double,double) 。对于第二次重载,第一个模板参数 RT 被指定为 double T1 7 T2 int c>从 4推导出为 double ,则实例化为 double max(int,double) 。然后,第二次重载会在重载分辨率中获胜,因为这是完全匹配,第一个需要从 int double 隐式转换第一个参数 7

Given ::max<double>(7, 4.), for the 1st overload, the 1st template parameter T1 is specified as double, and T2 is deduced as double from 4., so the instantiation would be double max(double, double). For the 2nd overload, the 1st template parameter RT is specified as double, T1 is deduced as int from 7, T2 is deduced as double from 4., then the instantiation would be double max(int, double). Then the 2nd overload wins in overload resolution because it's an exact match, the 1st one requires the implicit conversion from int to double for the 1st argument 7.

这篇关于模板功能的重载解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆