名稱空間
變體
操作

std::tuple_size<std:ranges::subrange>

來自 cppreference.com
< cpp‎ | ranges‎ | subrange
 
 
範圍庫 (Ranges library)
範圍介面卡 (Range adaptors)
 
 
定義於標頭檔案 <ranges>
template< class I, class S, ranges::subrange_kind K >

struct tuple_size<ranges::subrange<I, S, K>>

    : std::integral_constant<std::size_t, 2> {};
(C++20 起)

std::ranges::subrangestd::tuple_size 偏特化提供了一種編譯時方式,以元組式語法獲取 subrange 的元件數量,其始終為 2。這為結構化繫結提供支援。

目錄

繼承自 std::integral_constant

成員常量

value
[靜態]
常數值 2
(public static 成員常量)

成員函式

operator std::size_t
將物件轉換為 std::size_t,返回 value
(公開成員函式)
operator()
(C++14)
返回 value
(公開成員函式)

成員型別

型別 定義
value_type std::size_t
型別 std::integral_constant<std::size_t, value>

[編輯] 示例

#include <array>
#include <iostream>
#include <iterator>
#include <ranges>
 
int main()
{
    static_assert(2 == std::tuple_size_v<std::ranges::subrange<int*, int*>>);
 
    using array5 = std::array<int, 5>;
 
    static_assert(2 == std::tuple_size<std::ranges::subrange<
        array5::const_iterator, array5::const_iterator>>{});
 
    constexpr array5 a{1, 2, 3, 4, 5};
 
    std::ranges::subrange sub_a1{a};
 
    for (std::cout << "sub_a1: { "; int e : sub_a1)
        std::cout << e << ' ';
    std::cout << "}\n";
 
    std::ranges::subrange sub_a2{std::next(cbegin(a)), std::prev(cend(a))};
 
    const auto [first, last] = sub_a2;
    std::cout << "sub_a2 size = " << std::distance(first, last) << '\n';
 
    for (std::cout << "sub_a2: { "; int e : sub_a2)
        std::cout << e << ' ';
    std::cout << "}\n";
}

輸出

sub_a1: { 1 2 3 4 5 }
sub_a2 size = 3
sub_a2: { 2 3 4 }

[編輯] 參閱

結構化繫結 (C++17) 將指定的名稱繫結到初始化器的子物件或元組元素[編輯]
獲取類元組型別元素的數量
(類模板) [編輯]
獲取 tuple 的大小

一個 tuple
(類模板特化) [編輯]

獲取 pair 的大小
(類模板特化) [編輯]
獲得 array 的大小
(類模板特化) [編輯]
獲取 std::ranges::subrange 的迭代器或哨兵的型別
(類模板特化) [編輯]