std::integer_sequence
From cppreference.com
| ヘッダ <utility> で定義 |
||
| template< class T, T... Ints > class integer_sequence; |
(C++14以降) | |
クラステンプレート std::integer_sequence は、コンパイル時の整数のシーケンス (sequence) を表します。関数テンプレートの引数として使用すると、パラメータパック Ints が推論され、パック展開で使用できます。
目次 |
[編集] テンプレートパラメータ
| T | - | シーケンスの要素に使用する整数型 |
| ...Ints | - | シーケンスを表す非型パラメータパック |
[編集] メンバ型
| メンバ型 | 定義 |
value_type
|
T
|
[編集] メンバ関数
| size [static] |
Ints の要素数を返す(public static member function) |
std::integer_sequence::size
| static constexpr std::size_t size() noexcept; |
||
Ints 内の要素数を返します。sizeof...(Ints) と等価です。
パラメータ
(なし)
戻り値
Ints 内の要素数。
[編集] ヘルパーテンプレート
T が std::size_t である一般的なケースのために、ヘルパーエイリアステンプレート std::index_sequence が定義されています。
| template< std::size_t... Ints > using index_sequence = std::integer_sequence<std::size_t, Ints...>; |
||
ヘルパーエイリアステンプレート std::make_integer_sequence と std::make_index_sequence は、それぞれ Ints を 0, 1, 2, ..., N - 1 として std::integer_sequence と std::index_sequence 型を簡単に作成するために定義されています。
| template< class T, T N > using make_integer_sequence = std::integer_sequence<T, /* シーケンス 0, 1, 2, ..., N-1 */>; |
||
| template< std::size_t N > using make_index_sequence = std::make_integer_sequence<std::size_t, N>; |
||
N が負の場合、プログラムは不適格 (ill-formed) となります。N がゼロの場合、示される型は integer_sequence<T> となります。
ヘルパーエイリアステンプレート std::index_sequence_for は、任意の型パラメータパックを同じ長さのインデックスシーケンスに変換するために定義されています。
| template< class... T > using index_sequence_for = std::make_index_sequence<sizeof...(T)>; |
||
[編集] ノート
| 機能テストマクロ | 値 | 規格 | 機能 |
|---|---|---|---|
__cpp_lib_integer_sequence |
201304L |
(C++14) | コンパイル時整数シーケンス |
[編集] 例
別の例として、std::apply の実装例も参照してください。
このコードを実行
#include <array> #include <cstddef> #include <iostream> #include <tuple> #include <utility> namespace details { template <typename Array, std::size_t... I> constexpr auto array_to_tuple_impl(const Array& a, std::index_sequence<I...>) { return std::make_tuple(a[I]...); } 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...>) { ((os << (Is ? ", " : "") << std::get<Is>(t)), ...); } } template <typename T, T... ints> void print_sequence(int id, std::integer_sequence<T, ints...> int_seq) { std::cout << id << ") The sequence of size " << int_seq.size() << ": "; ((std::cout << ints << ' '), ...); std::cout << '\n'; } template <typename T, std::size_t N, typename Indx = std::make_index_sequence<N>> constexpr auto array_to_tuple(const std::array<T, N>& a) { return details::array_to_tuple_impl(a, Indx{}); } template <class Ch, class Tr, class... Args> auto& operator<<(std::basic_ostream<Ch, Tr>& os, const std::tuple<Args...>& t) { os << '('; details::print_tuple_impl(os, t, std::index_sequence_for<Args...>{}); return os << ')'; } int main() { print_sequence(1, std::integer_sequence<unsigned, 9, 2, 5, 1, 9, 1, 6>{}); print_sequence(2, std::make_integer_sequence<int, 12>{}); print_sequence(3, std::make_index_sequence<10>{}); print_sequence(4, std::index_sequence_for<std::ios, float, signed>{}); constexpr std::array<int, 4> array{1, 2, 3, 4}; auto tuple1 = array_to_tuple(array); static_assert(std::is_same_v<decltype(tuple1), std::tuple<int, int, int, int>>, ""); std::cout << "5) tuple1: " << tuple1 << '\n'; constexpr auto tuple2 = array_to_tuple<int, 4, std::integer_sequence<std::size_t, 1, 0, 3, 2>>(array); std::cout << "6) tuple2: " << tuple2 << '\n'; }
出力
1) The sequence of size 7: 9 2 5 1 9 1 6 2) The sequence of size 12: 0 1 2 3 4 5 6 7 8 9 10 11 3) The sequence of size 10: 0 1 2 3 4 5 6 7 8 9 4) The sequence of size 3: 0 1 2 5) tuple1: (1, 2, 3, 4) 6) tuple2: (2, 1, 4, 3)
[編集] 関連項目
| (C++20) |
組み込み配列から std::array オブジェクトを作成する(関数テンプレート) |
| (C++11)(C++17) |
指定された値を持つ、指定された型のコンパイル時定数 (クラステンプレート) |