c++ - 为什么 std::make_tuple(7 + N...) 在 C++11 中是合法的?

标签 c++ templates c++11 variadic-templates compile-time-constant

以下代码在 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/

相关文章:

c++ - 在 C++ 中确定其返回类型的函数

C++选择排序——插入方法和私有(private)变量

templates - 新 api 应用程序资源的 Azure 资源模板

c++ - 多个类的共享模板成员函数

c++ - "lazy man' s enable_if"是合法的 C++ 吗?

c++ - 特定类型列表的函数重载

c++ - Qt C++串口类的继承

c++ - std::array 类型初始化

Visual Studio 中的 C++ 文件扩展名

c - 将 C++11 和 C 库一起用于复数