std::ranges::view_interface<D>::size
来自cppreference.com
< cpp | ranges | view interface
| constexpr auto size() requires ranges::forward_range<D> && std::sized_sentinel_for<ranges::sentinel_t<D>, |
(1) | (C++20 起) |
| constexpr auto size() const requires ranges::forward_range<const D> && std::sized_sentinel_for<ranges::iterator_t<const D>, |
(2) | (C++20 起) |
size() 成员函数的默认实现通过计算哨位与起始迭代器间的差获得范围的大小。
1) 令
derived 为 static_cast<D&>(*this) 。等价于 return /*to-unsigned-like*/(ranges::end(derived) - ranges::begin(derived)); ,其中仅用于阐释的函数模板 to-unsigned-like 进行从参数类型到其对应的无符号类型的显式转型。2) 同 (1) ,除了
derived 为 static_cast<const D&>(*this) 。参数
(无)
返回值
派生类类型值的哨位与起始迭代器间的差,转换到其所对应的无符号类型。
注解
下列派生类型能使用 size() 的默认实现:
- std::ranges::drop_while_view
下列类型派生自 std::ranges::view_interface 并且不声明其自身的 size() 成员函数,但它们无法使用默认实现,因为其迭代器与哨位类型决不满足 sized_sentinel_for :
- std::ranges::basic_istream_view
- std::ranges::filter_view
- std::ranges::join_view
- std::ranges::split_view
- std::ranges::lazy_split_view
- std::ranges::take_while_view
缺陷报告
下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。
| 缺陷报告 | 应用于 | 出版时的行为 | 正确行为 |
|---|---|---|---|
| LWG 3646 | C++20 | size 函数的默认实现返回有符号类型
|
返回无符号类型 |
参阅
| (C++17)(C++20) |
返回容器或数组的大小 (函数模板) |
| (C++20) |
返回等于范围大小的整数 (定制点对象) |
| (C++20) |
返回等于范围大小的有符号整数 (定制点对象) |