std::incrementable_traits(3)
β¨ 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 aweakly_incrementabletype (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 thestd::common_iteratortype (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
- 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