名前空間
変種
操作

std::integer_sequence

From cppreference.com
< cpp‎ | utility
 
 
メタプログラミングライブラリ
型特性
型のカテゴリ
(C++11)
(C++11)(DR*)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11) 
(C++11)
(C++11)
型のプロパティ
(C++11)
(C++11)
(C++14)
(C++11)(C++26で非推奨)
(C++11)(C++20まで*)
(C++11)(C++20で非推奨)
(C++11)
型特性定数
メタ関数
(C++17)
サポートされている操作
関係とプロパティクエリ
型の変更
(C++11)(C++11)(C++11)
型の変換
(C++11)(C++23で非推奨)
(C++11)(C++23で非推奨)
(C++11)
(C++11)(C++20まで*)(C++17)

(C++11)
(C++17)
コンパイル時有理数演算
コンパイル時整数シーケンス
integer_sequence
(C++14)
 
ヘッダ <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 内の要素数。

[編集] ヘルパーテンプレート

Tstd::size_t である一般的なケースのために、ヘルパーエイリアステンプレート std::index_sequence が定義されています。

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

ヘルパーエイリアステンプレート std::make_integer_sequencestd::make_index_sequence は、それぞれ Ints0, 1, 2, ..., N - 1 として std::integer_sequencestd::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 オブジェクトを作成する
(関数テンプレート) [編集]
指定された値を持つ、指定された型のコンパイル時定数
(クラステンプレート) [編集]
English 日本語 中文(简体) 中文(繁體)