std::incrementable_traits(3)

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

✨ std::incrementable_traits ✨

okay so bestie, today we're talking about std::incrementable_traits from the C++ Standard Library β€” she's a class template and she's lowkey iconic for keeping difference types in check πŸ’… (the manual page lives under std::incrementable_traits(3), iykyk).


🌸 NAME 🌸

std::incrementable_traits β€” literally just std::incrementable_traits, no notes, that's her name, periodt πŸ’–


πŸ¦‹ SYNOPSIS πŸ¦‹

So here's the vibe β€” she's defined in the header <iterator>, and this particular specialization looks like this:

template< class I, class S >

struct incrementable_traits<std::common_iterator<I, S>> {  (since C++20)
    using difference_type = std::iter_difference_t<I>;

};

(yes, that (since C++20) is literally sitting right there in the synopsis β€” she's a C++20 girlie πŸŽ€)


🧚 DESCRIPTION 🧚

What does she even do? She provides the uniform interface to the associated difference type of the std::common_iterator type. That's it, that's the whole assignment, and she ate πŸ”₯

Like β€” std::common_iterator walks in, and std::incrementable_traits is right there handing it a difference_type equal to std::iter_difference_t<I>. Consistent. Clean. Main character energy fr fr πŸ‘‘


🌈 EXAMPLE 🌈

Run this code, bestie, it's giving "trust me bro, it compiles" ✨

// Run this code

#include <cstddef>
#include <iterator>
#include <list>
#include <string>
#include <type_traits>

int main()
{
    using CI = std::common_iterator<
                   std::counted_iterator<int*>,
                   std::default_sentinel_t>;
    using CL = std::common_iterator<
                   std::counted_iterator<std::list<std::string>::iterator>,
                   std::default_sentinel_t>;
    CL cl{std::default_sentinel};
    static_assert(
        std::same_as<std::incrementable_traits<CI>::difference_type,
std::ptrdiff_t> &&
        std::same_as<std::incrementable_traits<CL>::difference_type,
std::ptrdiff_t> &&
        std::same_as<std::incrementable_traits<decltype(cl)>::difference_type,
                     std::ptrdiff_t>);
}

like, all three static_asserts are checking that the difference_type comes out as std::ptrdiff_t every single time β€” and they all pass, no cap πŸ˜ŒπŸ’…


πŸŽ€ SEE ALSO πŸŽ€

the friend group πŸ’– here's who she's linking up with:

  • incrementable_traits (C++20) β€” computes the difference type of a weakly_incrementable type (class template)
  • iter_value_t (C++20)
  • iter_reference_t (C++20)
  • iter_const_reference_t (C++23)
  • iter_difference_t (C++20)
  • iter_rvalue_reference_t (C++20)
  • iter_common_reference_t (C++20)

β€” these ones compute the associated types of an iterator (alias template)

  • std::iterator_traits<std::common_iterator> (C++20) β€” provides uniform interface to the properties of the std::common_iterator type (class template specialization)

bestie reminder: the (C++20) and (C++23) tags are not decorative, they're real version markers β€” don't try to use these before your standard is ready, ok? 🫢

so that's the whole page, and honestly? she's short, she's sweet, she does exactly one thing and does it flawlessly β€” obsessed πŸ˜­πŸ’œ

signing off from http://cppreference.com, version 2024.06.10 of the std::incrementable_traits(3) page β€” see you in the next group chat, love u bye πŸ¦‹πŸ’–πŸŒΈ

πŸ“¦ where this page comes from