【发布时间】:2017-12-22 16:12:36
【问题描述】:
这是来自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.
问题一:arr 是什么?
问题2:n是一个int数组,是否匹配到int...N?
问题3:怎么会扩展成const char(&)[2], int(&)[1]
【问题讨论】:
-
查看关于包扩展的任意数量的问题,例如this one
-
解决问题 2:
Ts是数组类型的类型包,N是数组维度的 int 包,因此n将类型int提供到Ts包,并将维度 1 放入N包中。 -
使用可变参数模板引用 C-Array (
int (&a)[42]) 的丑陋语法。 -
对于 3:"a" 等价于 { 'a', 0 }
标签: c++ templates generic-programming