[英]Ellipsis appears in a parameter declaration of a template function
This is the example from cppreference . 这是cppreference的示例。 I don't understand how the pattern get expanded.
我不明白模式是如何扩展的。
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.
Question 1: what is arr? 问题1:什么是arr?
Question 2: n is a int array, does it match to int...N? 问题2:n是一个int数组,它与int ... N匹配吗?
Question 3: How come it can expand to const char (&)[2], int(&)[1] 问题3:为什么它可以扩展为const char(&)[2],int(&)[1]
Whereas 而
template <typename ...Ts> void f(Ts&...arr);
is mostly equivalent to 大多相当于
template <typename T0, typename T1, .., typename TN>
void f(T0& arr0, T1& arr1, .., TN& arrN);
for any N
. 任何
N
In the same way, 以同样的方式,
template <typename ...Ts, int... Ns> void g(Ts (&...arr)[Ns]);
would be equivalent to 相当于
template <typename T0, typename T1, .., typename TN, int N0, int N1, .. int NN>
void g(T0 (&arr0)[N0], T1 (&arr1)[N1], .., TN (&arrN)[NN]);
and type T (&)[N]
is a reference to C-array of size N
with element of type T
类型
T (&)[N]
是对大小为N
C-数组的引用,其中元素为T
类型
int n[1];
is trivially of type int [1]
. 通常类型为
int [1]
。
"a"
is of type const char[2]
( {'a', '\\0'}
). "a"
的类型为const char[2]
( {'a', '\\0'}
)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.