以下代码在 C++11 中是合法的。
template<int... N>
std::tuple<decltype(N)...> f()
{
return std::make_tuple(7 + N...);
}
什么意思?
最佳答案
首先看模板参数:template <int ... N>
.即使可以将可变数量的模板参数提供给 f
, 它们都必须是 int
类型.
现在当您使用 f<t1, t2, ..., tn>
, parameter unpacking (7 + N...)
将遵循模式 7 + N
并展开为
7 + t1, 7 + t2, 7 + t3, ..., 7 + tn
因此,您最终会得到一个元组,其中包含的每个模板参数都增加了 7。详细信息可以在第 14.5.3 节可变参数模板 [temp.variadic] 中找到。
3. 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 [...].
关于c++ - 为什么 std::make_tuple(7 + N...) 在 C++11 中是合法的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24647926/