命名空間
變體
動作

std::integer_sequence

出自 cppreference.com
< cpp‎ | 工具
 
 
元程式設計 (Metaprogramming) 函式庫
型別特徵
類型類別
(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++11)
(C++17)
編譯期有理數運算
編譯期整數序列
integer_sequence
(C++14)
 
定義於標頭檔 <utility>
template< class T, T... Ints >
class integer_sequence;
(C++14 起)

類別樣板 std::integer_sequence 代表一個編譯期的整數序列。當作為函式樣板的引數使用時,參數包 Ints 可以被推導出來,並用於包展開(pack expansion)。

目錄

[編輯] 樣板參數

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 物件
(函式樣板) [編輯]
指定型別與指定值的編譯期常數
(類別模板) [編輯]
English Deutsch 日本語 中文(简体) 中文(繁體)