std::tuple_element<std::ranges::subrange>

来自cppreference.com
< cpp‎ | ranges‎ | subrange
 
 
范围库
范围适配器
 
 
在标头 <ranges> 定义
template< class I, class S, ranges::subrange_kind K >
struct tuple_element<0, ranges::subrange<I, S, K>>;
(1) (C++20 起)
template< class I, class S, ranges::subrange_kind K >
struct tuple_element<0, const ranges::subrange<I, S, K>>;
(2) (C++20 起)
template< class I, class S, ranges::subrange_kind K >
struct tuple_element<1, ranges::subrange<I, S, K>>;
(3) (C++20 起)
template< class I, class S, ranges::subrange_kind K >
struct tuple_element<1, const ranges::subrange<I, S, K>>;
(4) (C++20 起)

std::tuple_element 针对 std::ranges::subrange 的部分特化,支持编译时用元组式语法访问 subrange 的迭代器或哨位类型。它们是为支持结构化绑定而提供的。

1,2) 获得迭代器类型,即 I
3,4) 获得哨位类型,即 S

成员类型

成员类型 定义
type (1,2) I
(3,4) S

注解

由于 subrangeget 函数按值返回迭代器与哨位,在 subrange 有 const 限定(但无 volatile 限定)时不对结果类型添加 const 限定符。

subrange 为 volatile 限定,则由于使用了针对 volatile 或 const volatile 类型的部分特化而结果类型亦为 volatile 限定。这种用法被弃用。

示例

#include <iterator>
#include <list>
#include <ranges>
#include <type_traits>
 
int main()
{
    std::list<int> list{3, 1, 4, 1, 5, 9, 2, 6};
 
    std::ranges::subrange subrange
    {
        std::counted_iterator{std::begin(list), 4},
        std::default_sentinel
    };
 
    static_assert(
        std::is_same_v<
            std::tuple_element_t<0, decltype(subrange)>,
            // 实现定义的类型:
            std::counted_iterator<std::_List_iterator<int>>
            >);
 
    static_assert(
        std::is_same_v<
            std::tuple_element_t<1, decltype(subrange)>,
            std::default_sentinel_t
            >);
}

参阅

结构化绑定 (C++17) 绑定指定的名字到初始化式的子对象或元组元素
获得元组式类型的元素类型
(类模板)
获得 std::ranges::subrange 的组分数量
(类模板特化)