在线文档教程
C++
应用 | Utilities

std::integer_sequence

STD::整数[医]序列

Defined in header
template< class T, T... Ints > class integer_sequence;(since C++14)

类模板std::integer_sequence表示整数的编译时间序列。当用作功能模板,参数包Ints可推导并用于封装展开。

模板参数

T-an integer type to use for the elements of the sequence
...Ints-a non-type parameter pack representing the sequence

成员类型

Member typeDefinition
value_typeT

成员函数

size staticreturns the number of elements in Ints (public static member function)

STD::整数[医]顺序:大小

static constexpr std::size_t size(

中的元素数。Ints相当于sizeof...(Ints)...

参数

%280%29

返回值

中的元素数。Ints...

例外

noexcept规格:

noexcept

辅助模板

助手别名模板std::index_sequence是为常见情况定义的Tstd::size_t...

template using index_sequence = std::integer_sequence;

助手别名模板std::make_integer_sequence定义为简化创建std::integer_sequencestd::index_sequence类型为0,1,2,...,N-1Ints*

template using make_integer_sequence = std::integer_sequence;
template<std::size_t N> using make_index_sequence = make_integer_sequence<std::size_t, N>;

如果程序格式不正确,则为N是阴性的。如果N为零,所指示的类型是integer_sequence<T>...

助手别名模板std::index_sequence_for定义为将任何类型参数包转换为相同长度的索引序列。

template using index_sequence_for = std::make_index_sequence;

二次

#include <tuple> #include <iostream> #include <array> #include <utility> // Convert array into a tuple template<typename Array, std::size_t... I> decltype(auto) a2t_impl(const Array& a, std::index_sequence<I...>) { return std::make_tuple(a[I]... } template<typename T, std::size_t N, typename Indices = std::make_index_sequence<N>> decltype(auto) a2t(const std::array<T, N>& a) { return a2t_impl(a, Indices() } // pretty-print a tuple (from http://stackoverflow.com/a/6245777/273767) template<class Ch, class Tr, class Tuple, std::size_t... Is> void print_tuple_impl(std::basic_ostream<Ch,Tr>& os, const Tuple & t, std::index_sequence<Is...>) { using swallow = int[]; // guarantees left to right order (void)swallow{0, (void(os << (Is == 0? "" : ", ") << std::get<Is>(t)), 0)...}; } template<class Ch, class Tr, class... Args> decltype(auto) operator<<(std::basic_ostream<Ch, Tr>& os, const std::tuple<Args...>& t) { os << "("; print_tuple_impl(os, t, std::index_sequence_for<Args...>{} return os << ")"; } int main() { std::array<int, 4> array = {1,2,3,4}; // convert an array into a tuple auto tuple = a2t(array static_assert(std::is_same<decltype(tuple), std::tuple<int, int, int, int>>::value, "" // print it to cout std::cout << tuple << '\n'; }

二次

产出:

二次

(1, 2, 3, 4)

二次

此示例演示如何std::tuple可以转换为函数调用%28的参数(参见std::experimental::apply29%。

二次

#include <iostream> #include <tuple> #include <utility> template<typename Func, typename Tup, std::size_t... index> decltype(auto) invoke_helper(Func&& func, Tup&& tup, std::index_sequence<index...>) { return func(std::get<index>(std::forward<Tup>(tup))... } template<typename Func, typename Tup> decltype(auto) invoke(Func&& func, Tup&& tup) { constexpr auto Size = std::tuple_size<typename std::decay<Tup>::type>::value; return invoke_helper(std::forward<Func>(func), std::forward<Tup>(tup), std::make_index_sequence<Size>{} } void foo(int a, const std::string& b, float c) { std::cout << a << " , " << b << " , " << c << '\n'; } int main() { auto args = std::make_tuple(2, "Hello", 3.5 invoke(foo, args }

二次

产出:

二次

2 , Hello , 3.5

二次

© cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

http://en.cppreference.com/w/cpp/实用程序/整型[医]序列