std::tuple_size<std::ranges::subrange>
来自 zh.cppreference.net
| 定义于头文件 <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::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
的迭代器或哨兵类型
(类模板特化) |