std:: tuple_size <std:ranges::subrange>
|
定义于头文件
<ranges>
|
||
|
template
<
class
I,
class
S,
ranges::
subrange_kind
K
>
struct
tuple_size
<
ranges::
subrange
<
I, S, K
>>
|
(C++20 起) | |
对
std::tuple_size
的
std::ranges::subrange
部分特化提供了在编译时获取
subrange
组件数量的方法(该值始终为2),这一特性通过类元组语法实现。此特化为结构化绑定支持而提供。
目录 |
继承自 std:: integral_constant
成员常量
|
value
[static]
|
常量值 2
(公开静态成员常量) |
成员函数
|
operator std::size_t
|
转换对象为
std::
size_t
,返回
value
(公开成员函数) |
|
operator()
(C++14)
|
返回
value
(公开成员函数) |
成员类型
| 类型 | 定义 |
value_type
|
std:: size_t |
type
|
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) | 将指定名称绑定到初始化器的子对象或元组元素 |
|
(C++11)
|
获取类元组类型的元素数量
(类模板) |
|
(C++11)
|
获取
|
|
(C++11)
|
获取
pair
的大小
(类模板特化) |
|
(C++11)
|
获取
array
的大小
(类模板特化) |
|
获取
std::ranges::subrange
的迭代器或哨兵类型
(类模板特化) |