std::forward_iterator(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’… std::forward_iterator β€” a page from the C++ Standard Libary manual, section 3

okay bestie, so this is the manual page for std::forward_iterator, straight out of the C++ Standard Libary manual (yes, section 3, she's official πŸ‘‘). Sit down, get a lil snack πŸ“, and let's get into it.


✨ NAME ✨

std::forward_iterator β€” literally just std::forward_iterator, no notes πŸ’€


πŸ¦‹ SYNOPSIS πŸ¦‹

So this concept lives in the header <iterator>, and here's exactly what she looks like:

template< class I >
concept forward_iterator =
    std::input_iterator<I> &&
    std::derived_from</*ITER_CONCEPT*/<I>, std::forward_iterator_tag> &&
    std::incrementable<I> &&
    std::sentinel_for<I, I>;

(since C++20) β€” she's a modern girlie, no cap ✨


🌸 DESCRIPTION 🌸

The way this concept just refines std::input_iterator… obsessed. Here's the tea:

  • she requires that I also models std::incrementable β€” which lowkey makes her suitable for multi-pass algorithms πŸ”₯
  • and she guarantees that two iterators to the same range can be compared against each other 🫢

Like, that's it, that's the whole vibe. Iconic behaviour, truly. πŸ‘‘


πŸ’œ NOTES πŸ’œ

okay this part matters, pay attention bestie πŸ™ˆ β€” unlike the LegacyForwardIterator requirements, the forward_iterator concept does not require dereference to return a reference. Not me being surprised by that… 😭 but fr fr, that's the actual rule.


🧚 EXAMPLE 🧚

here's a minimum forward iterator, and honestly she ate:

#include <cstddef>
#include <iterator>

struct SimpleForwardIterator
{
    using difference_type = std::ptrdiff_t;
    using value_type = int;

    int operator*() const;

    SimpleForwardIterator& operator++();

    SimpleForwardIterator operator++(int)
    {
        auto tmp = *this;
        ++*this;
        return tmp;
    }

    bool operator==(const SimpleForwardIterator&) const;
};

static_assert(std::forward_iterator<SimpleForwardIterator>);

the static_assert at the end? that's the vibe check, and she passed πŸ’…βœ¨


🌈 SEE ALSO 🌈

  • input_iterator (C++20) β€” specifies that a type is an input iterator, that is, its referenced values can be read and it can be both pre- and post-incremented (concept)
  • bidirectional_iterator (C++20) β€” specifies that a forward_iterator is a bidirectional iterator, supporting movement backwards (concept)

iykyk, the iterator family tree is giving main character energy πŸ‘‘


anyway that's the page!! source is http://cppreference.com, version 2024.06.10, and the date on the cover is 2024.06.10 too (she's stamped and dated, periodt). go forth and iterate forward bestie πŸ’–πŸŽ€πŸ¦‹

πŸ“¦ where this page comes from