Namespaces
Variants

std::ranges::iota_view<W, Bound>:: size

From cppreference.net
Ranges library
Range adaptors
constexpr auto size ( ) const

requires ( std:: same_as < W, Bound > && /*advanceable*/ < W > ) ||
( /*is-integer-like*/ < W > && /*is-integer-like*/ < Bound > ) ||

std:: sized_sentinel_for < Bound, 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()); // 错误:由于“u”是无界的,size()方法不存在
}

缺陷报告

以下行为变更缺陷报告被追溯应用于先前发布的C++标准。

缺陷报告 应用于 发布时的行为 正确行为
LWG 3610 C++20 size 可能拒绝整数类类型 尽可能接受

参见

返回等于范围大小的整数值
(定制点对象)
返回等于范围大小的有符号整数值
(定制点对象)