std::tuple_size<std:ranges::subrange>
来自cppreference.com
| 在标头 <ranges> 定义
|
||
| |
(C++20 起) | |
std::tuple_size 针对 std::ranges::subrange 的部分特化提供编译时获得 subrange 的组分数量的方式,该数始终为 2。它是为支持结构化绑定而提供的。
继承自 std::integral_constant
成员常量
value [静态] |
常量值 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 }
