Namespaces
Variants

std:: integer_sequence

From cppreference.net
Metaprogramming library
Type traits
Type categories
(C++11)
(C++11) ( DR* )
Type properties
(C++11)
(C++11)
(C++14)
(C++11) (deprecated in C++26)
(C++11) ( until C++20* )
(C++11) (deprecated in C++20)
(C++11)
Type trait constants
Metafunctions
(C++17)
Supported operations
Relationships and property queries
Type modifications
Type transformations
(C++11) (deprecated in C++23)
(C++11) (deprecated in C++23)
(C++11)
(C++11) ( until C++20* ) (C++17)

Compile-time rational arithmetic
Compile-time integer sequences
integer_sequence
(C++14)
ヘッダーで定義 <utility>
template < class T, T... Ints >
class integer_sequence ;
(C++14以降)

クラステンプレート std::integer_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 内の要素数。

ヘルパーテンプレート

共通的なケースにおいて、ヘルパーのエイリアステンプレート std::index_sequence が定義されています。ここで T std::size_t です:

template < std:: size_t ... Ints >
using index_sequence = std :: integer_sequence < std:: size_t , Ints... > ;

ヘルパーエイリアステンプレート std::make_integer_sequence および std::make_index_sequence は、それぞれ std::integer_sequence および std::index_sequence 型の作成を簡素化するために定義されており、 0 1 2 ... N - 1 Ints として持つ:

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) コンパイル時整数シーケンス

実装例

make_integer_sequence
namespace detail {
template<class T, T I, T N, T... integers>
struct make_integer_sequence_helper
{
    using type = typename make_integer_sequence_helper<T, I + 1, N, integers..., I>::type;
};
template<class T, T N, T... integers>
struct make_integer_sequence_helper<T, N, N, integers...>
{
    using type = std::integer_sequence<T, integers...>;
};
}
template<class T, T N>
using make_integer_sequence = detail::make_integer_sequence_helper<T, 0, N>::type;

関連項目 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 << ") サイズ " のシーケンス << 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) サイズ7のシーケンス: 9 2 5 1 9 1 6 
2) サイズ12のシーケンス: 0 1 2 3 4 5 6 7 8 9 10 11 
3) サイズ10のシーケンス: 0 1 2 3 4 5 6 7 8 9 
4) サイズ3のシーケンス: 0 1 2 
5) タプル1: (1, 2, 3, 4)
6) タプル2: (2, 1, 4, 3)

関連項目

(C++20)
組み込み配列から std::array オブジェクトを作成する
(関数テンプレート)
指定された型と値を持つコンパイル時定数
(クラステンプレート)