关于推导函数参数包的模板参数是否存在缺陷 [英] Is it a defect about deducing template arguments for function parameter pack

查看:77
本文介绍了关于推导函数参数包的模板参数是否存在缺陷的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

template<typename...T>
void func(T...args){
}
int main(){
  func(1,2.0,'c');
}

考虑上面的代码,有一个规则适用于此函数,以推断出这些模板参数模板(调用)。它是:

temp.deduct .call#1

Consider the above code, there's a rule that applied to it to deduce these template arguments for this function template (call). It is:
temp.deduct.call#1


对于出现在parameter-declaration-list末尾的函数参数包,将对每个剩余的参数进行推导调用的实参,将函数参数包的 declarator-id 的类型P作为相应的函数模板参数类型。每个推论都会推导模板参数包中由函数参数包扩展的后续位置的模板参数。

For a function parameter pack that occurs at the end of the parameter-declaration-list, deduction is performed for each remaining argument of the call, taking the type P of the declarator-id of the function parameter pack as the corresponding function template parameter type. Each deduction deduces template arguments for subsequent positions in the template parameter packs expanded by the function parameter pack.

这意味着参数声明 T ... args ,它声明了一个函数模板库,因此用于与函数参数类型相反的函数参数类型为 T 因为 ... args 是此声明的声明符ID。因此,对于此函数调用 func(1,2.0,'c'),模板参数包T将是由 {int,double ,char}

That means for the parameter-declaration T...args, it declares a function template park, hence the function parameter type that is used to against the type of function argument is T because ...args is the declarator-id of this declaration. So, for this function call func(1,2.0,'c'), template parameter pack T would be the set consists of {int,double,char}.

但是,请考虑以下变体:

However, consider the following variant:

template<typename...T>
void func(T...){
}
int main(){
  func(1,2.0,'c');
}

这里没有声明符-id,只是一个表示 ... ,在这种情况下如何使用引号?这里对应的功能参数类型是什么?如何形成此参数类型?在起草标准时是否存在这种情况的缺陷?

There's no declarator-id here, just an abstract-declarator that denote the ..., How would the quote be applied to this case? What's the corresponding function parameter type here? How to form this parameter type? Is it a defect for this case when draft the standard?

推荐答案

cppreference 它说明:


函数参数列表

Function parameter list

在函数参数列表中,如果在参数声明中出现省略号(无论是否命名为函数参数包(如Args ... args)),则参数声明为模式:

In a function parameter list, if an ellipsis appears in a parameter declaration (whether it names a function parameter pack (as in, Args ... args) or not) the parameter declaration is the pattern:


template<typename ...Ts> void f(Ts...) {}
f('a', 1);  // Ts... expands to void f(char, int)
f(0.1);     // Ts... expands to void f(double)

这篇关于关于推导函数参数包的模板参数是否存在缺陷的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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