Namespaces
Variants
Views
Actions

std::ranges::range

From cppreference.com
< cpp‎ | ranges
Revision as of 21:39, 7 September 2023 by 183.212.173.139 (Talk)

 
 
Ranges library
Range adaptors
 
Defined in header <ranges>
template< class T >

concept range = requires( T& t ) {
  ranges::begin(t); // equality-preserving for forward iterators
  ranges::end  (t);

};
(since C++20)

The range 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.

Semantic requirements

Given an expression E such that decltype((E)) is T, T models range only if

Note: In the definition above, the required expressions ranges::begin(t) and ranges::end(t) do not require implicit expression variations.

Notes

A typical range class only needs to provide two functions:

  1. A member function begin() whose return type models input_or_output_iterator.
  2. A member function end() whose return type models sentinel_for<It>, where It is the return type of begin().

Alternatively, they can be non-member functions, to be found by argument-dependent lookup.

Example

#include <iostream>
#include <ranges>
#include <vector>
 
template <typename T>
struct range_t : private T
{
    using T::begin, T::end; /*...*/
};
static_assert(std::ranges::range< range_t<std::vector<int>> >);
 
template <typename T> struct scalar_t
{
    T t {}; /* no begin/end */
};
static_assert(not std::ranges::range< scalar_t<int> >);
 
int main()
{
    if constexpr (range_t<std::vector<int>> r; std::ranges::range<decltype(r)>)
        std::cout << "r is a range\n";
 
    if constexpr (scalar_t<int> s; not std::ranges::range<decltype(s)>)
        std::cout << "s is not a range\n";
}

Output:

r is a range
s is not a range