std::sized_...sentinel_for(3)
πΈ 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):
sized_sentinel_foris a concept that specifies that an object of the iterator typeIand an object of the sentinel typeScan be subtracted to compute the distance between them in constant time β‘ like, O(1) behavior, we love to see it.disable_sized_sentinel_foris a variable template you can use to prevent iterators and sentinels that can be subtracted but do not actually modelsized_sentinel_forfrom satisfying the concept π it's the "no babe, not like that" switch.
π«Ά important nuance coming through: a program may specializedisable_sized_sentinel_forfor cv-unqualified non-array object typeSandI, as long as at least one of which is a program-defined type. Such specializations are usable in constant expressions and have typeconst 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
- 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