std::ranges::range(3)
π std::ranges::range(3) π
okay so bestie, we're cracking open the C++ Standard Libary manual (yes, that spelling is literally right there, don't @ me π) and the whole page is about std::ranges::range. No cap, this is the concept that checks whether a type is range-y enough to iterate over. It's giving main character energy for every container that wants to be looped over. Periodt. π
β¨ NAME β¨
std::ranges::range β std::ranges::range
π¦ Synopsis π¦
Defined in header <ranges>
template< class T >
concept range = requires( T& t ) {
ranges::begin(t); // equality-preserving for forward iterators
(since C++20)
ranges::end (t);
};
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. Slay.
πΈ Notes πΈ
A typical range class only needs to provide two functions:
- A member function
begin()whose return type modelsinput_or_output_iterator. - A member function
end()whose return type modelssentinel_for<It>, whereItis the return type ofbegin().
Alternatively, they can be non-member functions, to be found by argument-dependent lookup. iykyk π§
π Example π
#include <ranges>
// A minimum range
struct SimpleRange
{
int* begin();
int* end();
};
static_assert(std::ranges::range<SimpleRange>);
// not a range: no begin/end
struct NotRange
{
int t {};
};
static_assert(!std::ranges::range<NotRange>);
// not a range: begin does not return an input_or_output_iterator
struct NotRange2
{
void* begin();
int* end();
};
static_assert(!std::ranges::range<NotRange2>);
π Defect reports π
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
- DR:
LWG 3915 - Applied to:
C++20 - Behavior as published:
ranges::begin(t)andranges::end(t)did not require implicit expression variations - Correct behavior: removed the redundant description
That's the whole vibe check β .
source: http://cppreference.com β’ version: 2024.06.10 β’ page: std::ranges::range(3) β bye bestie π«Ά
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms