Scroll to navigation

std::ranges::views::iota,std::ranges::iota_view(3) C++ Standard Libary std::ranges::views::iota,std::ranges::iota_view(3)

NAME

std::ranges::views::iota,std::ranges::iota_view - std::ranges::views::iota,std::ranges::iota_view

Synopsis

Defined in header <ranges> template< std::weakly_incrementable W,

std::semiregular Bound = std::unreachable_sentinel_t > requires /*weakly-equality-comparable-with*/<W, Bound> && (1) (since C++20) std::copyable<W> class iota_view

: public ranges::view_interface<iota_view<W, Bound>> namespace views {

inline constexpr /* unspecified */ iota = /* unspecified */; (2) (since C++20)

} Call signature template< class W >

requires /* see below */ (since C++20)

constexpr /* see below */ iota( W&& value ); template< class W, class Bound >

requires /* see below */ (since C++20)

constexpr /* see below */ iota( W&& value, Bound&& bound );

1) A range factory that generates a sequence of elements by repeatedly incrementing an initial value. Can be either bounded or unbounded (infinite). 2) views::iota(e) and views::iota(e, f) are expression-equivalent to iota_view<std::decay_t<decltype((e))>>(e) and iota_view(e, f) respectively for any suitable subexpressions e and f.


Customization point objects

The name views::iota denotes a customization point object, which is a const function object of a literal semiregular class type. For exposition purposes, the cv-unqualified version of its type is denoted as __iota_fn.

All instances of __iota_fn are equal. The effects of invoking different instances of type __iota_fn on the same arguments are equivalent, regardless of whether the expression denoting the instance is an lvalue or rvalue, and is const-qualified or not (however, a volatile-qualified instance is not required to be invocable). Thus, views::iota can be copied freely and its copies can be used interchangeably.

Given a set of types Args..., if std::declval<Args>()... meet the requirements for arguments to views::iota above, __iota_fn models


* std::invocable<__iota_fn, Args...>,
* std::invocable<const __iota_fn, Args...>,
* std::invocable<__iota_fn&, Args...>, and
* std::invocable<const __iota_fn&, Args...>.

Otherwise, no function call operator of __iota_fn participates in overload resolution.


Data members

Member Definition W value_ the beginning value
(exposition-only member object*) Bound bound_ the sentinel value, may be unreachable
(exposition-only member object*)

Member functions

constructor creates an iota_view
(public member function) begin obtains the beginning iterator of an iota_view
(public member function) end obtains the sentinel denoting the end of an iota_view
(public member function)
tests whether the iota_view is empty (i.e. the iterator and the sentinel empty compare equal)
(public member function) size obtains the size of an iota_view (only provided if it is bounded) (optional) (public member function)
Inherited from std::ranges::view_interface cbegin returns a constant iterator to the beginning of the range. (C++23) (public member function of std::ranges::view_interface<D>) cend returns a sentinel for the constant iterator of the range. (C++23) (public member function of std::ranges::view_interface<D>)
returns whether the derived view is not empty. Provided if ranges::empty is operator bool applicable to it.
(public member function of std::ranges::view_interface<D>)
returns the first element in the derived view. Provided if it satisfies front forward_range.
(public member function of std::ranges::view_interface<D>)
returns the last element in the derived view. Provided if it satisfies back bidirectional_range and common_range.
(public member function of std::ranges::view_interface<D>)
returns the n^th element in the derived view. Provided if it satisfies operator[] random_access_range.
(public member function of std::ranges::view_interface<D>)


Deduction guides


Nested classes

iterator the iterator type
(exposition-only member class*)
the sentinel type used when the iota_view is bounded and Bound and W are not the sentinel same type
(exposition-only member class*)


Helper templates

template< std::weakly_incrementable W, std::semiregular Bound > constexpr bool ranges::enable_borrowed_range<ranges::iota_view<W, Bound>> = (since C++20) true;

This specialization of ranges::enable_borrowed_range makes iota_view satisfy borrowed_range.

Example

// Run this code

#include <algorithm> #include <iostream> #include <ranges>

struct Bound {
int bound;
bool operator==(int x) const { return x == bound; } };

int main() {
for (int i : std::ranges::iota_view{1, 10})
std::cout << i << ' ';
std::cout << '\n';


for (int i : std::views::iota(1, 10))
std::cout << i << ' ';
std::cout << '\n';


for (int i : std::views::iota(1, Bound{10}))
std::cout << i << ' ';
std::cout << '\n';


for (int i : std::views::iota(1) | std::views::take(9))
std::cout << i << ' ';
std::cout << '\n';


std::ranges::for_each(std::views::iota(1, 10),
[](int i){ std::cout << i << ' '; });
std::cout << '\n'; }

Output:

1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9


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 4096 C++20 views::iota could copy an iota_view as-is forbidden P2325R3 C++20 iota_view required that W is semiregular only requires that W is
as view required default_initializable copyable

See also

iota fills a range with successive increments of the starting value (C++11) (function template) ranges::iota fills a range with successive increments of the starting value (C++23) (algorithm function object) ranges::repeat_view a view consisting of a generated sequence by repeatedly producing the views::repeat same value (C++23) (class template) (customization point object)

2026.11.10 http://cppreference.com