c++ - 省略号出现在模板函数的参数声明中

标签 c++ templates generic-programming

这是 cppreference 中的示例。我不明白该模式是如何扩展的。

template<typename ...Ts, int... N> void g(Ts (&...arr)[N]) {}
int n[1];
g<const char, int>("a", n); // Ts (&...arr)[N] expands to 
                            // const char (&)[2], int(&)[1]

Note: In the pattern Ts (&...arr)[N], the ellipsis is the innermost element, not the last element as in all other pack expansions.

问题1:arr是什么?

问题2:n是一个int数组,它与int...N匹配吗?

问题3:为什么可以扩展为const char (&)[2], int(&)[1]

最佳答案

鉴于

template <typename ...Ts> void f(Ts&...arr);

基本上相当于

template <typename T0, typename T1, .., typename TN>
void f(T0& arr0, T1& arr1, .., TN& arrN);

对于任何N .

同理,

template <typename ...Ts, int... Ns> void g(Ts (&...arr)[Ns]);

相当于

template <typename T0, typename T1, .., typename TN, int N0, int N1, .. int NN>
void g(T0 (&arr0)[N0], T1 (&arr1)[N1], .., TN (&arrN)[NN]);

并输入T (&)[N]是对大小为 N 的 C 数组的引用元素类型为 T

int n[1];类型很简单 int [1] .

"a"类型为const char[2] ({'a', '\0'})。

关于c++ - 省略号出现在模板函数的参数声明中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45155833/

相关文章:

c++ - 带有 std::conditional 的模板函数自动返回类型

数据/集合中的 Racket 序列与内置序列

c++ - sf::音乐播放但不是 sf::声音?为什么?

c++ - 为什么我不能将这条记录插入mysql数据库?

c++ - 获取在 C++ 中创建的 Box2D 主体以与 QML 中的 Box2D 主体发生碰撞

c++ - 通过接口(interface)检索未知类型数据的类型安全方法

使用函数指针的 C++ 模板参数

C:将任何函数传递给函数

c++ - 带转换运算符的模板参数

c++ - 将元组转换为模板中的结构