当模板参数推导是带有默认参数的类模板时,如何对函数模板参数进行模板推导 [英] How does the template argument deduction perform for function template parameter when it is a class template with default argument

查看:66
本文介绍了当模板参数推导是带有默认参数的类模板时,如何对函数模板参数进行模板推导的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

template<typename T, typename U = T>
struct Test{};

template<typename T>
void func(Test<T>){  //#1
}

int main(){
  func(Test<int>{});  //#2
}

考虑以上代码,在调用功能模板 func 时,参数类型为 Test< int,int> ,在调用功能模板时,模板参数推导将执行.

Consider the above code, At the point of invocation of function template func, the type of argument is Test<int,int>, When call the function template, template argument deduction will perform.

函数调用的模板参数推导的规则是:
temp.deduct#call-1

The rule of template argument deduction for function call is :
temp.deduct#call-1

模板参数推导是通过将每个包含参与模板参数推导的模板参数的函数模板参数类型(称为P)与调用的相应参数的类型(称为A)进行比较来完成的,如下所述.

Template argument deduction is done by comparing each function template parameter type (call it P) that contains template-parameters that participate in template argument deduction with the type of the corresponding argument of the call (call it A) as described below.

我很确定 A 的类型是 Test< int,int> ,但是我不确定 P的类型在这里.是 Test< T> 还是 Test< T,T> ,根据规则,这里看来 P 的类型是 Test< T> ,然后进行推导过程以确定参与模板参数推导的 T 的值.然后根据这些规则描述如下:

I'm pretty sure the type of A is Test<int,int>, however I'm not sure what the type of P here is. Is it Test<T> or Test<T,T>, According to the rule, It seems to the type of P here is Test<T>, then deduction process is performed to determine the value of T that participate in template argument deduction. Then according to these rules described as the following:

temp.deduct#call-4

通常,推论过程会尝试查找模板推论值,以使推论的A与A相同(在如上所述对类型A进行转换之后).

In general, the deduction process attempts to find template argument values that will make the deduced A identical to A (after the type A is transformed as described above).

temp.deduct#5

从默认模板参数推导或获得所有模板参数后,模板的模板参数列表和函数类型中所有对模板参数的使用都将替换为相应的推导或默认参数值.

When all template arguments have been deduced or obtained from default template arguments, all uses of template parameters in the template parameter list of the template and the function type are replaced with the corresponding deduced or default argument values.

因为类模板 Test 具有默认参数,因此推论出的 T 被替换为默认参数.这意味着推导的 A Test< int,int> ,并且与参数类型 Test< int,int> 相同.

Because the class template Test has a default argument, hence the deduced T is substituted into default argument. That means the deduced A is Test<int,int> and it is identical to Argument type Test<int,int>.

但是,这只是我的理解.我不确定 P 的类型.如果将函数参数的类型更改为 Test< int,double> ,则结果将报告:

However, It's just my understanding. I'm not sure what type the P here is. If change the type of function argument to Test<int,double>, the outcome will report:

candidate template ignored: deduced conflicting types for parameter 'T' ('int' vs. 'double')

结果看起来好像 P Test< T,T> 并且 T 的第一值与第二个冲突 T 的值.

The outcome looks like as if the P is Test<T,T> and the fist value of T is conflicting with the second value of T.

所以,我的问题是:

这里的 P Test< T> 还是 Test< T,T> ?为什么?

Whether the P here is Test<T> or Test<T,T>? and why?

推荐答案

不是语言律师的答案

没有类型 Test< T> 实际上是简写"形式.用于 Test< T,T> .

There is no type Test<T> is actually a "shorthand" for Test<T, T>.

与默认函数参数一样,如果您具有 int foo(int a,int b = 24),则函数的类型为 int(int,int)并且像 foo(11)这样的任何呼叫实际上都是 foo(11,24).

Just like with default function arguments if you have int foo(int a, int b = 24) the type of the function is int (int, int) and any call like foo(11) is actually foo(11, 24).

这篇关于当模板参数推导是带有默认参数的类模板时,如何对函数模板参数进行模板推导的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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