- utility[meta header]
- std[meta namespace]
- class template[meta id-type]
- cpp14[meta cpp]
namespace std {
template <class T, T... I>
struct integer_sequence {
using value_type = T;
static constexpr std::size_t size() noexcept { return sizeof...(I); }
};
}integer_sequenceは、任意の整数型のシーケンスをコンパイル時に表現するクラスである。
このクラスは、tupleオブジェクトを展開して、引数パックとして他の関数に転送することを主目的として作られた。
Tは整数型であること。
| 名前 | 説明 | 対応バージョン |
|---|---|---|
tuple_size |
静的な要素数取得(class template) | C++26 |
tuple_element |
静的な要素の型取得(class template) | C++26 |
| 名前 | 説明 | 対応バージョン |
|---|---|---|
get |
指定したインデックスの整数値を取得する | C++26 |
- C++17まで : テンプレートパラメータ
Tが整数型でない場合の動作は未定義。 - C++20から :
Tが整数型でない場合、プログラムは不適格となり、コンパイルエラーとなることが要求されるようになった。
#include <iostream>
#include <utility>
void g(int a, int b, int c)
{
std::cout << a << ", " << b << ", " << c << std::endl;
}
template <class T, T... Seq>
void f(std::integer_sequence<T, Seq...>)
{
// 定数のシーケンス{0, 1, 2}を取り出して、関数g()の引数として転送
g(Seq...);
}
int main()
{
f(std::integer_sequence<int, 0, 1, 2>());
}- std::integer_sequence[color ff0000]
0, 1, 2
#include <iostream>
#include <utility>
int main()
{
// 構造化束縛で整数シーケンスをパックとして取り出す
constexpr auto [...Index] = std::make_index_sequence<3>();
((std::cout << Index << ' '), ...);
std::cout << std::endl;
// template for文で整数シーケンスを直接イテレートする
template for (constexpr std::size_t I : std::make_index_sequence<3>()) {
std::cout << I << ' ';
}
std::cout << std::endl;
}- std::make_index_sequence[link make_index_sequence.md]
0 1 2
0 1 2
- C++14
- Clang: 3.4 [mark verified]
- GCC: 4.9.0 [mark verified]
- ICC: ??
- Visual C++: 2015 [mark verified]
- N3658 Compile-time integer sequences
- P1460R1 Mandating the Standard Library: Clause 20 - Utilities library
- P1789R3 Library Support for Expansion Statements
- C++26で構造化束縛と
template for文で使用できるよう、tuple_size/tuple_element/getの特殊化を追加
- C++26で構造化束縛と