std::tuple_element<std::ranges::subrange>
来自cppreference.com
| 在标头 <ranges> 定义
|
||
| |
(1) | (C++20 起) |
| |
(2) | (C++20 起) |
| |
(3) | (C++20 起) |
| |
(4) | (C++20 起) |
std::tuple_element 针对 std::ranges::subrange 的部分特化,支持编译时用元组式语法访问 subrange 的迭代器或哨位类型。它们是为支持结构化绑定而提供的。
1,2) 获得迭代器类型,即
I。3,4) 获得哨位类型,即
S。成员类型
| 成员类型 | 定义 |
type
|
(1,2) I(3,4) S
|
注解
由于 subrange 的 get 函数按值返回迭代器与哨位,在 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
>);
}
