std::experi...es::distance(3)
πΈ NAME πΈ
okay so bestie, welcome to the std::experi...es::distance(3) page from the C++ Standard Libary manual β yes, that's the manual name, spelling and all, and we are not fixing it π
. The NAME section is doing a little echo thing: std::experimental::ranges::distance β and then, for emphasis, std::experimental::ranges::distance again. It's giving main character energy, periodt.
This is the ranges TS distance customization point object, aka the little guy that tells you how far apart two iterators are or how big a range is. Not me being obsessed with iterators, but I am.
β¨ Synopsis β¨
Defined in header <experimental/ranges/iterator>.
The page shows the customization point object as:
namespace {
constexpr /* unspecified */ distance = /* unspecified */;
}
and tags it with (ranges TS) and (customization point object) β both annotations are right there in the original, no cap.
π Call signature π
Here are the overloads, exactly as listed:
- Overload
(1):
template< Iterator I, Sentinel<I> S >
constexpr ranges::difference_type_t<I> distance( I first, S last );
- Overload
(2):
template< Range R >
constexpr ranges::difference_type_t<ranges::iterator_t<R>>
distance( R&& r );
- Overload
(3):
template< SizedRange R >
constexpr ranges::difference_type_t<ranges::iterator_t<R>>
distance( R&& r );
So what does it do? It returns the distance between first and last, or between the beginning and the end of the range r. That's the whole vibe. π¦
For overload (1):
- If
SizedSentinel<S, I>is satisfied, it's equivalent toreturn last - first;. - Otherwise, it returns the number of increments needed to get from
firsttolast. - If
[first, last)does not denote a range, thenIandSmust be the same type and must modelSizedSentinel, and[last, first)must denote a range. Otherwise, the behavior is undefined. Like, undefined behavior undefined. Do not play with that, bestie π.
For overload (2):
- It's equivalent to
return ranges::distance(ranges::begin(r), ranges::end(r));.
For overload (3):
- It's equivalent to
return ranges::size(r);.
β οΈ actual warning from the page: instantiating overloads(2,3)may be ill-formed if the header<experimental/ranges/range>is not included before the point of instantiation. So yes, include that header first, bestie. Fr fr.
π Return value π
The distance between first and last, or between the beginning and the end of the range r. That's it. That's the tea. π
π Example π
This section is incomplete.
Reason: no example.
Not me staring at an empty example section like ποΈπποΈ. It's literally incomplete, so no example to copy β that's the official status, periodt.
π₯ See also π₯
distanceβ returns the distance between two iterators (function template).advanceβ advances an iterator by given distance (function template).nextβ increment an iterator (function template).prevβ decrement an iterator (function template).sizeβ obtains the size of a range whose size can be calculated in constant time (customization point object).
π¦ Category π¦
- Todo no example.
okay bye bestie, stay safe and don't instantiate overloads (2,3) without <experimental/ranges/range> π«Ά. This page is from cppreference.com, dated 2024.06.10 β mwah πβ¨
π¦ 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