Namespaces
Variants

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

From cppreference.com
 
 
Ranges library
Range adaptors
 
 
iota_view() requires std::default_initializable<W> = default;
(1) (since C++20)
constexpr explicit iota_view( W value );
(2) (since C++20)
constexpr explicit iota_view( std::type_identity_t<W> value,
                              std::type_identity_t<Bound> bound );
(3) (since C++20)
constexpr explicit iota_view( /*iterator*/ first, /* see below */ last );
(4) (since C++20)

Constructs an iota_view.

1) Default constructor. Value-initializes the underlying value_ and bound_.
2) Initializes value_ with value and value-initializes bound_.
  • The behavior is undefined if:
  • Bound is not std::unreachable_sentinel_t and Bound() is not reachable from value.
  • std::totally_ordered_with<W, Bound> is modeled, and bool(value <= Bound()) is false.
3) Initializes value_ with value and bound_ with bound.
  • The behavior is undefined if:
  • Bound is not std::unreachable_sentinel_t and bound is not reachable from value.
  • std::totally_ordered_with<W, Bound> is modeled, and bool(value <= bound) is false.
4) Initializes value_ with the W value stored in first, and initializes bound_ with the Bound value represented by last (which has the same type as that of end()).
  • if W and Bound are the same type, equivalent to iota_view(first.value_, last.value_).
  • Otherwise, if Bound is std::unreachable_sentinel_t, equivalent to iota_view(first.value_, last).
  • Otherwise, equivalent to iota_view(first.value_, last.bound_).

Parameters

value - the starting value
bound - the bound
first - the iterator denoting the starting value
last - the iterator or sentinel denoting the bound

Example

#include <cassert>
#include <iostream>
#include <iterator>
#include <ranges>

int main()
{
    const auto l = {1, 2, 3, 4};

    auto i1 = std::ranges::iota_view<int, int>(); // overload (1)
    assert(i1.empty() and i1.size() == 0);

    auto i2 = std::ranges::iota_view(1); // overload (2)
    assert(not i2.empty() and i2.front() == 1);
    for (std::cout << "1) "; auto e : i2 | std::views::take(3))
        std::cout << e << ' ';
    std::cout << '\n';

    auto i3 = std::ranges::iota_view(std::begin(l)); // overload (2)
    assert(not i3.empty() and i3.front() == l.begin());
    for (std::cout << "2) "; auto e : i3 | std::views::take(4))
        std::cout << *e << ' ';
    std::cout << '\n';

    auto i4 = std::ranges::iota_view(1, 8); // overload (3)
    assert(not i4.empty() and i4.front() == 1 and i4.back() == 7);
    for (std::cout << "3) "; auto e : i4)
        std::cout << e << ' ';
    std::cout << '\n';

    auto i5 = std::ranges::iota_view(l.begin(), l.end()); // overload (4)
    for (std::cout << "4) "; auto e : i5)
        std::cout << *e << ' ';
    std::cout << '\n';

    auto i6 = std::ranges::iota_view(l.begin(), std::unreachable_sentinel); // (4)
    for (std::cout << "5) "; auto e : i6 | std::views::take(3))
        std::cout << *e << ' ';
    std::cout << '\n';
}

Output:

1) 1 2 3
2) 1 2 3 4
3) 1 2 3 4 5 6 7
4) 1 2 3 4
5) 1 2 3

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 3523 C++20 iterator-sentinel pair constructor (4) might use wrong sentinel type corrected
P2711R1 C++20 the multi-parameter constructors (3,4) were not explicit made explicit