std::ranges::iota_view<W, Bound>::size
来自 cppreference.cn
constexpr auto size() const requires (std::same_as<W, Bound> && /*advanceable*/<W>) || |
(自 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()); // Error: size() is not present since “u” is unbounded }
[编辑] 缺陷报告
下列行为变更缺陷报告被追溯应用于先前发布的 C++ 标准。
DR | 应用于 | 已发布行为 | 正确行为 |
---|---|---|---|
LWG 3610 | C++20 | size 可能拒绝整数类类型 |
如果可能则接受 |
[编辑] 参见
(C++20) |
返回等于范围大小的整数 (定制点对象) |
(C++20) |
返回等于范围大小的有符号整数 (定制点对象) |