std::sized_...sentinel_for(3)

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

🌸 std::sized_sentinel_for, std::disable_sized_sentinel_for 🌸

okay bestie, welcome to the <iterator> page of the C++ Standard Libary (yes that's the manual's spelling, i'm not touching it πŸ’…) β€” we're talking std::sized_sentinel_for and std::disable_sized_sentinel_for, two absolute icons of the C++20 era ✨


πŸ’– NAME πŸ’–

std::sized_sentinel_for, std::disable_sized_sentinel_for β€” no cap, that's literally the whole name section, we move.


πŸ¦‹ Synopsis πŸ¦‹

okay so first things first, all of this lives in the header <iterator> πŸŽ€

template< class S, class I >
    concept sized_sentinel_for =
        std::sentinel_for<S, I> &&
        !std::disable_sized_sentinel_for<std::remove_cv_t<S>,
                                         std::remove_cv_t<I>> &&  (1)
(since C++20)
        requires(const I& i, const S& s) {
            { s - i } -> std::same_as<std::iter_difference_t<I>>;
            { i - s } -> std::same_as<std::iter_difference_t<I>>;

        };
template< class S, class I >                                      (2)
(since C++20)
    inline constexpr bool disable_sized_sentinel_for = false;

she's giving ✨two-for-one✨ and both of them are (since C++20):

  1. sized_sentinel_for is a concept that specifies that an object of the iterator type I and an object of the sentinel type S can be subtracted to compute the distance between them in constant time ⚑ like, O(1) behavior, we love to see it.
  2. disable_sized_sentinel_for is a variable template you can use to prevent iterators and sentinels that can be subtracted but do not actually model sized_sentinel_for from satisfying the concept πŸ™ˆ it's the "no babe, not like that" switch.
🫢 important nuance coming through: a program may specialize disable_sized_sentinel_for for cv-unqualified non-array object type S and I, as long as at least one of which is a program-defined type. Such specializations are usable in constant expressions and have type const bool. periodt. πŸ‘‘

πŸ‘‘ See also πŸ‘‘

  • ranges::sized_range (C++20) β€” (concept) specifies that a range knows its size in constant time 🌈
  • ranges::size (C++20) β€” (customization point object) returns an integer equal to the size of a range πŸ“

and that's the whole page bestie, mwah πŸ’‹ the source on this one is http://cppreference.com, stamped 2024.06.10 β€” and the manual name is std::sized_...sentinel_for(3) if you were wondering, iykyk πŸ’œ πŸŽ€

πŸ“¦ where this page comes from