std::experi...erence_type(3)

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

πŸ’– NAME πŸ’–

std::experimental::ranges::difference_type β€” std::experimental::ranges::difference_type, bestie. We're deep in the C++ Standard Libary manual (yes, β€œLibary” is literally how it's spelled in the header, don't come for me πŸ’€), and the title up top is all truncated like std::experi...erence_type(3). Anyway, this whole page is about that one template. Periodt.


πŸ¦‹ Synopsis πŸ¦‹

Defined in header <experimental/ranges/iterator>:

          template< class I >
       (1)
          struct difference_type {};
          template< class T >
       (2)
          struct difference_type<T*>;
          template< class T >
       (3)
          struct difference_type<const T> : difference_type<std::decay_t<T>>
       {};
          template< class T >

              requires requires { typename T::difference_type; }
       (4)

          struct difference_type<T>;
          template< class T >

              requires !requires { typename T::difference_type; } &&
       (5)
                       requires(const T& a, const T& b) { {a - b} -> Integral;
       }

          struct difference_type<T>;

So it computes the associated difference type of the type I, if any. Users may specialize difference_type for a program-defined type. Slay.

Here's the full breakdown, no skipping:

  1. Primary template is an empty struct. πŸ’€
  2. Specialization for pointers. If T is an object type, provides a member type type equal to std::ptrdiff_t. Otherwise, there is no member type. No cap.
  3. Specialization for const-qualified types. Simple, iconic.
  4. Specialization for types that define a public and accessible member type difference_type. Provides a member type type equal to T::difference_type.
  5. Specialization for types that do not define a public and accessible member type difference_type but do support subtraction. Provides a member type type equal to std::make_signed_t<decltype(std::declval<T>() - std::declval<T>())>. The way this is so specific 😭

✨ Helper alias template ✨

template< class T >
(ranges TS)
using difference_type_t = typename ranges::difference_type<T>::type;

That's the (ranges TS) cutie right there. Obsessed.


πŸ’… Example πŸ’…

This section is incomplete 😭

Reason: no example.


🌈 See also 🌈

  • WeaklyIncrementable (concept) β€” specifies that a Semiregular type can be incremented with pre- and post-increment operators. Iconic.
  • iterator_traits (alias template) β€” compatibility traits class that collects an iterator’s associated types. We love a compatibility queen.

πŸŽ€ Category πŸŽ€

Category: * Todo no example


Mwah πŸ’‹ β€” sourced from http://cppreference.com, dated 2024.06.10, and the footer still says std::experi...erence_type(3). Stay slaying, bestie 🫢

πŸ“¦ where this page comes from