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::sentinel_t<const D>, |
(2) | (C++20 起) |
size()
成員函式的預設實現透過計算哨兵與起始迭代器之間的差值來獲取範圍的大小。
目錄 |
[編輯] 返回值
1)
to-unsigned-like
(ranges::end(static_cast<D&>(this)) -
ranges::begin(static_cast<D&>(this)))。2)
to-unsigned-like
(ranges::end(static_cast<const D&>(this)) -
ranges::begin(static_cast<const D&>(this)))。[編輯] 注意
以下派生型別可以使用 size()
的預設實現
以下型別派生自 std::ranges::view_interface 且未宣告自己的 size()
成員函式,但它們不能使用預設實現,因為它們的迭代器和哨兵型別從不滿足 sized_sentinel_for
- std::ranges::basic_istream_view
- std::ranges::filter_view
- std::ranges::join_view
- std::ranges::lazy_split_view
- std::ranges::split_view
- std::ranges::take_while_view
[編輯] 缺陷報告
下列更改行為的缺陷報告追溯地應用於以前出版的 C++ 標準。
缺陷報告 | 應用於 | 釋出時的行為 | 正確的行為 |
---|---|---|---|
LWG 3646 | C++20 | size 函式的預設實現返回有符號型別 |
它們返回無符號型別 |
[編輯] 參閱
(C++17)(C++20) |
返回容器或陣列的大小 (函式模板) |
(C++20) |
返回等於範圍大小的整數 (定製點物件) |
(C++20) |
返回等於範圍大小的有符號整數 (定製點物件) |