std::ranges::iota_view<W, Bound>::size - cppreference.com (original) (raw)
| 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>; | | (since C++20) | | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------- | | ------------- |
Returns the size of the view if the view is bounded.
For the definitions of /*advanceable*/ and /*is-integer-like*/, see _[advanceable](iterator.html#advanceable "cpp/ranges/iota view/iterator")_ and is-integer-like respectively.
[edit] Return value
If any of W and Bound is not a integer-like type, returns _[to-unsigned-like](../../ranges.html#to-unsigned-like "cpp/ranges")_ (_[bound](../iota%5Fview.html#bound "cpp/ranges/iota view")_ - _[value](../iota%5Fview.html#value "cpp/ranges/iota view")_ ).
Otherwise, returns (_[value](../iota%5Fview.html#value "cpp/ranges/iota view")_ < 0) ?( (_[bound](../iota%5Fview.html#bound "cpp/ranges/iota view")_ < 0) ?`_[to-unsigned-like](../../ranges.html#to-unsigned-like "cpp/ranges")_` (\-`_[value](../iota%5Fview.html#value "cpp/ranges/iota view")_` ) \-` ` `_[to-unsigned-like](../../ranges.html#to-unsigned-like "cpp/ranges")_` (\-`_[bound](../iota%5Fview.html#bound "cpp/ranges/iota view")_` ) : _[to-unsigned-like](../../ranges.html#to-unsigned-like "cpp/ranges")_ (_[bound](../iota%5Fview.html#bound "cpp/ranges/iota view")_ ) + _[to-unsigned-like](../../ranges.html#to-unsigned-like "cpp/ranges")_ (-_[value](../iota%5Fview.html#value "cpp/ranges/iota view")_ )) : _[to-unsigned-like](../../ranges.html#to-unsigned-like "cpp/ranges")_ (_[bound](../iota%5Fview.html#bound "cpp/ranges/iota view")_ ) - _[to-unsigned-like](../../ranges.html#to-unsigned-like "cpp/ranges")_ (_[value](../iota%5Fview.html#value "cpp/ranges/iota view")_ ) .
[edit] Example
#include #include 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 }
[edit] Defect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
| DR | Applied to | Behavior as published | Correct behavior |
|---|---|---|---|
| LWG 3610 | C++20 | size might reject integer-class types | accept if possible |
[edit] See also
| | returns an integer equal to the size of a range(customization point object)[edit] | | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | | returns a signed integer equal to the size of a range(customization point object)[edit] |