名稱空間
變體
操作

std::integer_sequence

來自 cppreference.com
< cpp‎ | 工具
 
 
超程式設計庫
型別特性
型別類別
(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 表示一個編譯時整數序列。當用作函式模板的引數時,引數包 Ints 可以被推導並在包擴充套件中使用。

目錄

[編輯] 模板引數

T - 用於序列元素的整數型別
...Ints - 表示序列的非型別引數包

[編輯] 成員型別

成員型別 定義
value_type T

[編輯] 成員函式

size
[靜態]
返回 Ints 中的元素數量
(公共靜態成員函式)

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 被定義為分別簡化 std::integer_sequencestd::index_sequence 型別的建立,其中 Ints0, 1, 2, ..., N - 1

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 為負數,則程式格式錯誤。如果 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 物件
(函式模板) [編輯]
指定型別和指定值的編譯時常量
(類模板) [編輯]