c++ - 推导函数参数包的模板参数是否有缺陷

标签 c++ templates c++17 language-lawyer

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

考虑上面的代码,有一个规则适用于它来为这个函数模板(调用)推导这些模板参数。它是:
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.

这意味着对于参数声明T...args,它声明了一个函数模板park,因此用于对抗函数参数类型的函数参数类型是T 因为 ...args 是这个声明的 declarator-id。因此,对于此函数调用 func(1,2.0,'c'),模板参数包 T 将是由 {int,double,char} 组成的集合。

但是,请考虑以下变体:

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

这里没有 declarator-id,只有一个表示 ... 的抽象声明符,如何将引用应用于这种情况?这里对应的函数参数类型是什么?如何形成这个参数类型?这种情况在起草标准时是否存在缺陷?

最佳答案

cppreference它解释说:

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)

关于c++ - 推导函数参数包的模板参数是否有缺陷,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62649169/

相关文章:

c++ - include guard没有生效

c++ - 使用折叠表达式合并多个 vector

c++ - 实现 std::iterator

C++ 将 std::tuple<char, char, char> 转换为 std::string?

c++ - 头文件中函数原型(prototype)的语法

c++ - 使用构造函数的变量

C# : Pass int array to c++ dll

c++ - 在模板方法中使用模板类中的模板类

c++ - 无法推断可变参数模板的模板类型

c++ - 使用 Search 方法为普通类型 (int) 和指针的 const 创建数组类