std::incrementable(3)

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

πŸ’… NAME πŸ’…

std::incrementable β€” bestie, it's literally std::incrementable ✨

πŸ¦‹ Synopsis πŸ¦‹

Okay so first things first, this whole thing lives in the header <iterator> πŸŽ€

template< class I >

    concept incrementable =
        std::regular<I> &&
        std::weakly_incrementable<I> &&  (since C++20)
        requires(I i) {
            { i++ } -> std::same_as<I>;

        };

So what this concept actually does β€” no cap β€” is specify requirements on types that can be incremented with the pre- and post-increment operators 🫢. Their increment operations have to be equality-preserving, and the type has to be std::equality_comparable. That's the vibe check πŸ’….

And like, here's the glow-up: std::weakly_incrementable only supports single-pass algorithms 😭, but types that model std::incrementable can be used with multi-pass one-directional algorithms. She leveled up πŸ‘‘.

✨ Semantic requirements ✨

I models std::incrementable only if, given any two incrementable objects a and b of type I:

  • bool(a == b) implies bool(a++ == b), and
  • bool(a == b) implies bool(((void)a++, a) == ++b).

Periodt πŸ’€ those are the rules.

🌸 Equality preservation 🌸

Expressions declared in requires expressions of the standard library concepts are required to be equality-preserving (except where stated otherwise). Not me reminding you again, but yeah πŸ’–.


⭐ See also ⭐

  • weakly_incrementable (C++20) (concept) β€” specifies that a semiregular type can be incremented with pre- and post-increment operators
  • same_as (C++20) (concept) β€” specifies that a type is the same as another type

okay that's the whole page bestie, go forth and increment responsibly πŸ§šπŸ’œ

signed, sealed, delivered with love πŸ’Œ from http://cppreference.com, version/date 2024.06.10 β€” that's the std::incrementable(3) era, iykyk πŸŽ€πŸ”₯

πŸ“¦ where this page comes from