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 type | Definition |
---|---|
value_type | T |
成员函数
size static | returns 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
是为常见情况定义的T
是std::size_t
...
template | | |
---|
助手别名模板std::make_integer_sequence
定义为简化创建std::integer_sequence
和std::index_sequence
类型为0,1,2,...,N-1
如Ints
*
template | | |
---|---|---|
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 | | |
---|
例
二次
#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::apply
29%。
二次
#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。