| ||||||||||||||||||||||
| Range primitives | |||||||
| |||||||
| Range concepts | |||||||||||||||||||
| |||||||||||||||||||
| Range factories | |||||||||
| |||||||||
| Range adaptors | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Helper items | |||||||||||||||||
| |||||||||||||||||
Defined in header <ranges> | ||
template<class T> concept range= requires( T& t){ | (since C++20) | |
Therange concept defines the requirements of a type that allows iteration over its elements by providing an iterator and sentinel that denote the elements of the range.
Contents |
Given an expressionE such thatdecltype((E)) isT,T modelsrange only if
[ranges::begin(E), ranges::end(E)) denotes arange, andforward_iterator,ranges::begin(E) isequality-preserving (in other words, forward iterators support multi-pass algorithms).A typicalrange class only needs to provide two functions:
begin() whose return type modelsinput_or_output_iterator.end() whose return type modelssentinel_for<It>, whereIt is the return type ofbegin().Alternatively, they can be non-member functions, to be found byargument-dependent lookup.
#include <ranges> // A minimum rangestruct SimpleRange{int* begin();int* end();};static_assert(std::ranges::range<SimpleRange>); // Not a range: no begin/endstruct NotRange{int t{};};static_assert(!std::ranges::range<NotRange>); // Not a range: begin does not return an input_or_output_iteratorstruct NotRange2{void* begin();int* end();};static_assert(!std::ranges::range<NotRange2>); int main(){}
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
| DR | Applied to | Behavior as published | Correct behavior |
|---|---|---|---|
| LWG 3915 | C++20 | ranges::begin(t) andranges::end(t) did not require implicit expression variations | removed the redundant description |