std::ranges::forward_range(3)

πŸ“¦
version 2024.07.05 Β· 3 versions
, in nixpkgs unstable, opensuse-leap 16.0

πŸ’… std::ranges::forward_range

okay so this is a page from the C++ Standard Library manual, bestie β€” yes, the whole lib, and this one little concept is our main character today. sit down, hydrate, let's go 🫢


✨ NAME ✨

std::ranges::forward_range β€” which is, and I cannot stress this enough, std::ranges::forward_range πŸ’« (the manpage really said "name: the name" and honestly? iconic confidence.)


🌸 SYNOPSIS 🌸

it lives right here, in the header <ranges>:

template< class T >
concept forward_range =

and this part is since C++20 ⭐:

    ranges::input_range<T> &&
    std::forward_iterator<ranges::iterator_t<T>>;

πŸŽ€ DESCRIPTION πŸŽ€

so like. forward_range? it's literally a refinement of range, no cap. it's the vibe check that says: when you call ranges::begin on this thing, what you get back is a model of forward_iterator. that's it. that's the whole concept. she's simple, she's precise, she ate πŸ’…βœ¨

think of it as: input range energy, but upgraded β€” you can walk through it more than once and it won't ghost you πŸ¦‹


🧚 EXAMPLE 🧚

run this code, bestie β€” the comments said so:

// Run this code

#include <forward_list>
#include <queue>
#include <ranges>
#include <span>
#include <stack>
#include <tuple>

const char* str{"not a forward range"};
const char str2[] = "a forward range";
static_assert(
    std::ranges::forward_range<decltype("a forward range")> &&
    !std::ranges::forward_range<decltype(str)> &&
    std::ranges::forward_range<decltype(str2)> &&
    !std::ranges::forward_range<std::stack<char>> &&
    std::ranges::forward_range<std::forward_list<char>> &&
    !std::ranges::forward_range<std::tuple<std::forward_list<char>>> &&
    std::ranges::forward_range<std::span<char>> &&
    !std::ranges::forward_range<std::queue<char>> &&
"");

int main() {}

look at all those static_asserts doing their little checks πŸ₯Ή yes the trailing "" is in there, don't touch it, it's part of the artifact β€” copy it exactly character for character, okay? πŸ”₯

and int main() {} β€” an empty main that does absolutely nothing and still gets to be in the docs. main character energy fr fr πŸ‘‘


🩷 tiny note: that static_assert block ends with ""); β€” keep it verbatim when you paste this. no fixing, no tidying. the manpage said what it said πŸ’€

🌈 SEE ALSO 🌈

ngl there's no SEE ALSO section in this one β€” she stands alone, independent, unbothered πŸ§šβ€β™€οΈ


and that's the page!! source is http://cppreference.com, the version stamp situation is 2024.06.10, and the page answers to std::ranges::forward_range(3) 🌸 love that for her, love that for you, mwah πŸ’–πŸŽ€

πŸ“¦ where this page comes from