Iterator concepts | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Iterator primitives | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Algorithm concepts and utilities | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Indirect callable concepts | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Common algorithm requirements | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Utilities | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Iterator adaptors | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
Defined in header <iterator> | ||
struct unreachable_sentinel_t; | (1) | (since C++20) |
inlineconstexpr unreachable_sentinel_t unreachable_sentinel{}; | (2) | (since C++20) |
unreachable_sentinel_t
is an empty class type that can be used to denote the “upper bound” of an unbounded interval.unreachable_sentinel
is a constant of typeunreachable_sentinel_t
.Contents |
operator== (C++20) | compares anunreachable_sentinel_t with a value of anyweakly_incrementable type(function template) |
template<std::weakly_incrementable I> friendconstexprbool operator==( unreachable_sentinel_t,const I&)noexcept | (since C++20) | |
unreachable_sentinel_t
can be compared with anyweakly_incrementable
type and the result is alwaysfalse.
This function template is not visible to ordinaryunqualified orqualified lookup, and can only be found byargument-dependent lookup whenstd::unreachable_sentinel_t
is an associated class of the arguments.
#include <concepts>#include <cstddef>#include <iterator>#include <ranges>#include <utility> namespace ranges= std::ranges; // never checks “iter != r.end()”template<ranges::random_access_range R>constexprstd::size_t trivial_strlen(R&& r){auto iter= r.begin();while(*iter!=ranges::range_value_t<R>{})++iter;return iter- r.begin();} template<ranges::random_access_range R>constexprstd::size_t my_strlen(R&& r){ifconstexpr(std::same_as<ranges::sentinel_t<R>, std::unreachable_sentinel_t>)return trivial_strlen(std::forward<R>(r));elsereturnranges::find(std::forward<R>(r),ranges::range_value_t<R>{})-ranges::begin(r);} int main(){constexprstaticchar str[]="The quick brown fox jumps over a lazy dog."; static_assert(my_strlen(str)==42); // finds the length of the string faster, but UB if “str” is not null-terminatedconstexprauto unsafe_str=ranges::subrange{str, std::unreachable_sentinel}; static_assert(my_strlen(unsafe_str)==42);}
(C++20) | aview consisting of a sequence generated by repeatedly incrementing an initial value(class template)(customization point object)[edit] |