Difference between revisions of "cpp/ranges/iota view"
m (→Data members: {{dsc expos...}}) |
(Added anchors to data members.) |
||
(16 intermediate revisions by 4 users not shown) | |||
Line 6: | Line 6: | ||
template< std::weakly_incrementable W, | template< std::weakly_incrementable W, | ||
std::semiregular Bound = std::unreachable_sentinel_t > | std::semiregular Bound = std::unreachable_sentinel_t > | ||
− | requires | + | requires /*weakly-equality-comparable-with*/<W, Bound> && std::copyable<W> |
class iota_view | class iota_view | ||
: public ranges::view_interface<iota_view<W, Bound>> | : public ranges::view_interface<iota_view<W, Bound>> | ||
Line 29: | Line 29: | ||
@1@ A range factory that generates a sequence of elements by repeatedly incrementing an initial value. Can be either bounded or unbounded (infinite). | @1@ A range factory that generates a sequence of elements by repeatedly incrementing an initial value. Can be either bounded or unbounded (infinite). | ||
− | @2@ {{c|views::iota(e)}} and {{c|views::iota(e, f)}} are [[cpp/language/expressions#Expression-equivalence|expression-equivalent]] to {{c|iota_view(e)}} and {{c|iota_view(e, f)}} respectively for any suitable subexpressions {{c|e}} and {{c|f}}. | + | @2@ {{c|views::iota(e)}} and {{c|views::iota(e, f)}} are [[cpp/language/expressions#Expression-equivalence|expression-equivalent]] to {{c|iota_view<std::decay_t<decltype((e))>>(e)}} and {{c|iota_view(e, f)}} respectively for any suitable subexpressions {{c|e}} and {{c|f}}. |
{{cpp/ranges/cpo|views|iota}} | {{cpp/ranges/cpo|views|iota}} | ||
Line 35: | Line 35: | ||
===Data members=== | ===Data members=== | ||
{{dsc begin}} | {{dsc begin}} | ||
− | {{dsc hitem|Member | + | {{dsc hitem|Member|Definition}} |
− | {{dsc expos mem obj| | + | {{dsc expos mem obj|spec={{tt|W}}|value_|id=value|the beginning value}} |
− | {{dsc expos mem obj| | + | {{dsc expos mem obj|spec={{tt|Bound}}|bound_|id=bound|the sentinel value, may be unreachable}} |
{{dsc end}} | {{dsc end}} | ||
===Member functions=== | ===Member functions=== | ||
{{dsc begin}} | {{dsc begin}} | ||
− | {{dsc mem ctor| | + | {{dsc mem ctor|cpp/ranges/iota_view/iota_view|creates an {{tt|iota_view}}}} |
− | {{dsc mem fun| | + | {{dsc mem fun|cpp/ranges/iota_view/begin|obtains the beginning iterator of an {{tt|iota_view}}}} |
− | {{dsc mem fun| | + | {{dsc mem fun|cpp/ranges/iota_view/end|obtains the sentinel denoting the end of an {{tt|iota_view}}}} |
− | {{dsc mem fun| | + | {{dsc mem fun|cpp/ranges/iota_view/empty|tests whether the {{tt|iota_view}} is empty (i.e. the iterator and the sentinel compare equal)}} |
+ | {{dsc mem fun|cpp/ranges/iota_view/size|notes={{mark optional}}|obtains the size of an {{tt|iota_view}} (only provided if it is bounded)}} | ||
− | {{cpp/ranges/view_interface/inherit|embedded=yes|data=invalid|size=yes}} | + | {{cpp/ranges/view_interface/inherit|embedded=yes|data=invalid|empty=invalid|size=yes}} |
{{dsc end}} | {{dsc end}} | ||
− | + | ==={{rl|deduction guides|Deduction guides}}=== | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
===Nested classes=== | ===Nested classes=== | ||
{{dsc begin}} | {{dsc begin}} | ||
− | {{dsc expos mem class|cpp/ranges/iota_view/iterator | + | {{dsc expos mem class|cpp/ranges/iota_view/iterator|the iterator type}} |
− | {{dsc expos mem class|cpp/ranges/iota_view/sentinel | + | {{dsc expos mem class|cpp/ranges/iota_view/sentinel|the sentinel type used when the {{tt|iota_view}} is bounded and {{tt|Bound}} and {{tt|W}} are not the same type}} |
{{dsc end}} | {{dsc end}} | ||
Line 167: | Line 62: | ||
{{ddcl|since=c++20|1= | {{ddcl|since=c++20|1= | ||
template< std::weakly_incrementable W, std::semiregular Bound > | template< std::weakly_incrementable W, std::semiregular Bound > | ||
− | + | constexpr bool ranges::enable_borrowed_range<ranges::iota_view<W, Bound>> = true; | |
}} | }} | ||
− | This specialization of {{ | + | This specialization of {{c/core|ranges::enable_borrowed_range}} makes {{tt|iota_view}} satisfy {{lconcept|borrowed_range}}. |
===Example=== | ===Example=== | ||
Line 202: | Line 97: | ||
std::cout << '\n'; | std::cout << '\n'; | ||
− | std::ranges::for_each(std::views::iota(1, 10), [](int i) | + | std::ranges::for_each(std::views::iota(1, 10), |
− | + | [](int i){ std::cout << i << ' '; }); | |
− | + | ||
− | + | ||
std::cout << '\n'; | std::cout << '\n'; | ||
} | } | ||
Line 218: | Line 111: | ||
===Defect reports=== | ===Defect reports=== | ||
{{dr list begin}} | {{dr list begin}} | ||
− | {{dr list item|wg=lwg|dr= | + | {{dr list item|wg=lwg|dr=4096|std=C++20|before={{tt|views::iota}} could copy an {{tt|iota_view}} as-is|after=forbidden}} |
− | + | ||
− | + | ||
{{dr list item|paper=P2325R3|std=C++20|before={{tt|iota_view}} required that {{tt|W}} is {{lconcept|semiregular}}<br>as {{lconcept|view}} required {{lconcept|default_initializable}}|after=only requires that {{tt|W}} is {{lconcept|copyable}}}} | {{dr list item|paper=P2325R3|std=C++20|before={{tt|iota_view}} required that {{tt|W}} is {{lconcept|semiregular}}<br>as {{lconcept|view}} required {{lconcept|default_initializable}}|after=only requires that {{tt|W}} is {{lconcept|copyable}}}} | ||
{{dr list end}} | {{dr list end}} |
Latest revision as of 19:02, 7 October 2024
Defined in header <ranges>
|
||
template< std::weakly_incrementable W, std::semiregular Bound = std::unreachable_sentinel_t > |
(1) | (since C++20) |
namespace views { inline constexpr /* unspecified */ iota = /* unspecified */; |
(2) | (since C++20) |
Call signature |
||
template< class W > requires /* see below */ |
(since C++20) | |
template< class W, class Bound > requires /* see below */ |
(since C++20) | |
Contents |
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.
[edit] 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*) |
[edit] Member functions
creates an iota_view (public member function) | |
obtains the beginning iterator of an iota_view (public member function) | |
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 compare equal) (public member function) | |
(optional) |
obtains the size of an iota_view (only provided if it is bounded) (public member function) |
Inherited from std::ranges::view_interface | |
(C++23) |
returns a constant iterator to the beginning of the range. (public member function of std::ranges::view_interface<D> )
|
(C++23) |
returns a sentinel for the constant iterator of the range. (public member function of std::ranges::view_interface<D> )
|
returns whether the derived view is not empty. Provided if ranges::empty is applicable to it. (public member function of std::ranges::view_interface<D> )
| |
returns the first element in the derived view. Provided if it satisfies forward_range . (public member function of std::ranges::view_interface<D> )
| |
returns the last element in the derived view. Provided if it satisfies 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 random_access_range . (public member function of std::ranges::view_interface<D> )
|
[edit] Deduction guides
[edit] Nested classes
the iterator type (exposition-only member class*) | |
the sentinel type used when the iota_view is bounded and Bound and W are not the same type(exposition-only member class*) |
[edit] Helper templates
template< std::weakly_incrementable W, std::semiregular Bound > constexpr bool ranges::enable_borrowed_range<ranges::iota_view<W, Bound>> = true; |
(since C++20) | |
This specialization of ranges::enable_borrowed_range makes iota_view
satisfy borrowed_range
.
[edit] Example
#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
[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 4096 | C++20 | views::iota could copy an iota_view as-is
|
forbidden |
P2325R3 | C++20 | iota_view required that W is semiregular as view required default_initializable
|
only requires that W is copyable
|
[edit] See also
(C++11) |
fills a range with successive increments of the starting value (function template) |
(C++23) |
fills a range with successive increments of the starting value (niebloid) |
a view consisting of a generated sequence by repeatedly producing the same value(class template) (customization point object) |