std::ranges::iota_view<W, Bound>::size
来自cppreference.com
| |
(C++20 起) | |
如果视图有界则返回视图的大小。
/*advanceable*/ 和 /*is-integer-like*/ 的定义分别见 advanceable 和 is-integer-like 。
返回值
如果 W 和 Bound 中有非整数式类型,那么返回 to-unsigned-like (bound_ - value_ )。
否则返回
(value_ < 0) ?
(
(bound_ < 0) ?
to-unsigned-like (-value_ ) - to-unsigned-like (-bound_ ) :
to-unsigned-like (bound_ ) + to-unsigned-like (-value_ )
) :
to-unsigned-like (bound_ ) - to-unsigned-like (value_ )
。
示例
运行此代码
#include <cassert>
#include <ranges>
int main()
{
unsigned initial_value{1}, bound{5};
auto i{std::views::iota(initial_value, bound)};
assert(i.size() == bound - initial_value and i.size() == 4);
auto u{std::views::iota(8)};
// assert(u.size()); // 错误:没有 size(),因为 u 无界
}
缺陷报告
下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。
| 缺陷报告 | 应用于 | 出版时的行为 | 正确行为 |
|---|---|---|---|
| LWG 3610 | C++20 | size 可能会拒绝整数类类型
|
使之在可能时接受 |
参阅
(C++20) |
返回等于范围大小的整数 (定制点对象) |
(C++20) |
返回等于范围大小的有符号整数 (定制点对象) |