std::ranges::iota_view<W, Bound>::iota_view - cppreference.com

From cppreference.com

iota_view()requiresstd::default_initializable<W>=default; (1) (since C++20)constexprexplicitiota_view(Wvalue); (2) (since C++20)constexprexplicitiota_view(std::type_identity_t<W>value,std::type_identity_t<Bound>bound); (3)(since C++20)constexprexplicitiota_view(/*iterator*/first,/* see below */last); (4)(since C++20)Constructs an

iota_view

.

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

Run this code

#include<cassert>#include<iostream>#include<iterator>#include<ranges>intmain(){constautol={1,2,3,4};autoi1=std::ranges::iota_view<int,int>();// overload (1)assert(i1.empty()andi1.size()==0);autoi2=std::ranges::iota_view(1);// overload (2)assert(noti2.empty()andi2.front()==1);for(std::cout<<"1) ";autoe:i2|std::views::take(3))std::cout<<e<<' ';std::cout<<'\n';autoi3=std::ranges::iota_view(std::begin(l));// overload (2)assert(noti3.empty()andi3.front()==l.begin());for(std::cout<<"2) ";autoe:i3|std::views::take(4))std::cout<<*e<<' ';std::cout<<'\n';autoi4=std::ranges::iota_view(1,8);// overload (3)assert(noti4.empty()andi4.front()==1andi4.back()==7);for(std::cout<<"3) ";autoe:i4)std::cout<<e<<' ';std::cout<<'\n';autoi5=std::ranges::iota_view(l.begin(),l.end());// overload (4)for(std::cout<<"4) ";autoe:i5)std::cout<<*e<<' ';std::cout<<'\n';autoi6=std::ranges::iota_view(l.begin(),std::unreachable_sentinel);// (4)for(std::cout<<"5) ";autoe: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