Is it a defect about deducing template arguments for function parameter pack

孤人 提交于 2020-08-07 21:36:53

问题


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

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

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.

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');
}

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?


回答1:


In cppreference it explains:

Function parameter list

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)


来源:https://stackoverflow.com/questions/62649169/is-it-a-defect-about-deducing-template-arguments-for-function-parameter-pack

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!