这是 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/