这里是有问题的习语:
template<typename... T>
void f(T... t) {
int temp[] = {(g(t), 0)...};
}
这将被编译为 g(t0); g(t1); ...
,函数调用的顺序由 C++11[dcl.init.list]/4 保证。
更好的版本使用 std::initializer_list
而不是数组,但这在这里并不重要。
问题是:我们应该如何称呼这个成语?
更新:
基本上,这是我们应该建议人们使用它而不是递归的成语,即替换两个重载
void f() {}
void f(H head, T... tail) { g(head); f(尾...); }
单例
void f(T... t) { int temp[]{(g(t), 0)...};
当然我们可以称它为“将被Fold Expressions取代的成语”,但我希望它有一个合适的称呼。
最佳答案
包扩展。
[expr.prim.lambda] 中的 C++11 §5.1.2/23:
” A capture followed by an ellipsis is a pack expansion (14.5.3). [Example:
template<class... Args> void f(Args... args) { auto lm = [&, args...] { return g(args...); }; lm(); }
—end example ]
我认为这涵盖了它。不应用函数的包展开可以看作是应用恒等函数。
[temp.variadic] 中的 C++11 §14.5.3/4:
A pack expansion consists of a pattern and an ellipsis, the instantiation of which produces zero or more instantiations of the pattern in a list (described below). […]
关于c++ - 如何调用使用数组将函数应用于可变参数包的成语,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28887549/