std::ranges::view_interface<D>::operator[]

From cppreference.com
 
 
Ranges library
Range access
Range conversions
(C++23)
Range primitives



Dangling iterator handling
Range concepts
Views

Range factories
Range adaptors
Range generators
Range adaptor objects
Range adaptor closure objects
Helper items
(until C++23)(C++23)


 
std::ranges::view_interface
Member functions
(C++20)
(C++23)
(C++23)
(C++20)
(C++20)
(C++20)
(C++20)
operator[]
(C++20)
 
template<ranges::random_access_range R = D>
constexpr decltype(auto) operator[]( ranges::range_difference_t<R> n );
(1) (since C++20)
template<ranges::random_access_range R = const D>
constexpr decltype(auto) operator[]( ranges::range_difference_t<R> n ) const;
(2) (since C++20)

The default implementation of operator[] member function obtains the element at the specified offset relative to the beginning iterator, reusing the operator[] of the iterator type.

1) Let derived be static_cast<D&>(*this). Equivalent to return ranges::begin(derived)[n];.
2) Same as (1), except that derived is static_cast<const D&>(*this).

Parameters

n - position of the element to return

Return value

The element at offset n relative to the beginning iterator.

Notes

In C++20, no type derived from std::ranges::view_interface in the standard library provides their own operator[] member function.

However, following derived types cannot use the default implementations, as they never satisfy random_access_range:

The inherited operator[] member function is available for std::ranges::empty_view, but a call to it always results in undefined behavior.

Example