省略号出现在模板函数的参数声明中(Ellipsis appears in a parameter declaration of a template function)

编程入门 行业动态 更新时间:2024-10-26 16:34:24
省略号出现在模板函数的参数声明中(Ellipsis appears in a parameter declaration of a template function)

这是来自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]

This is the example from 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?

Question 2: n is a int array, does it match to int...N?

Question 3: How come it can expand to 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'} )。

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.

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

int n[1]; is trivially of type int [1].

"a" is of type const char[2] ({'a', '\0'}).

更多推荐

本文发布于:2023-08-06 15:38:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1451591.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:省略号   出现在   函数   声明   模板

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!