命名空间
变体
操作

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

来自 cppreference.cn
< 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 的迭代器或哨兵的类型
(类模板特化) [编辑]