Namespaces
Variants

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

ja.cppreference.net より
 
 
Ranges ライブラリ
Range アダプタ
 
 
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 のいずれかが integer-like type でない場合、 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++規格に対して遡及的に適用されました。

DR 適用対象 公開時の動作 正しい動作
LWG 3610 C++20 size が整数クラス型を拒否する可能性がある 可能な場合は受け入れる

関連項目

範囲のサイズに等しい整数を返す
(カスタマイゼーションポイントオブジェクト)
範囲のサイズに等しい符号付き整数を返す
(カスタマイゼーションポイントオブジェクト)