std::common...:operator++(3)
πΈ NAME πΈ
std::common_iterator::operator++ - std::common_iterator::operator++ β yes bestie, that's the whole NAME line, and it's giving main character energy. This is the std::common...:operator++(3) page from the C++ Standard Libary, so buckle up: we're talking about incrementing a common_iterator, and it's lowkey iconic. π
β¨
β¨ Synopsis β¨
constexpr common_iterator& operator++(); (1)
(since C++20)
constexpr decltype(auto) operator++( int ); (2)
(since C++20)
π§ Helper types π§
This helper is exposition only (that's the (3) vibe), so don't expect it to show up as a regular part of the interface, bestie. π«Ά
class /*postfix_proxy*/ {
std::iter_value_t<I> keep_;
constexpr postfix_proxy(std::iter_reference_t<I>&& x)
: keep_(std::forward<std::iter_reference_t<I>>(x)) {}
public: (3)
(exposition
constexpr const std::iter_value_t<I>& operator*() const
only*)
noexcept {
return keep_;
}
};
Okay so here's the tea: Increments the underlying iterator. π₯
π¨ The behavior is undefined if the underlyingstd::variantmember objectvardoes not hold an object of typeI, i.e.std::holds_alternative<I>(var)is equal tofalse. That's a hard no, bestie β do not pass go, do not collect cute iterator vibes. π
Let it denote the iterator of type I held by var, that is std::get<I>(var).
- Pre-increments by one. Equivalent to:
++it; return *this;
- Post-increments by one:
- Equivalent to:
auto tmp = *this; ++*this; return tmp;
if I models forward_iterator.
- Equivalent to:
return it++;
if the variable definition auto&& ref = *it++; is well-formed, or either
std::indirectly_readable<I>orstd::constructible_from<std::iter_value_t<I>, std::iter_reference_t<I>>orstd::move_constructible<std::iter_value_t<I>>
is false.
- Equivalent to:
postfix_proxy p(**this); ++*this; return p;
otherwise, where postfix_proxy is an exposition only helper type (3).
π Parameters π
(none) β literally zero parameters. Not a single one. Slay. π
π Return value π
*this- A copy of
*thisthat was made before the change, or a result of post-increment of the underlying iterator, or a proxy keeping the value of the current element, as described above.
π Example π
#include <algorithm>
#include <initializer_list>
#include <iostream>
#include <iterator>
int main()
{
const auto il = {1, 2, 3, 4, 5, 6};
using CI = std::common_iterator<
std::counted_iterator<std::initializer_list<int>::iterator>,
std::default_sentinel_t
>;
CI first{std::counted_iterator{std::begin(il), std::ssize(il) -
2}};
for (; first != std::default_sentinel; ++first)
std::cout << *first << ' ';
std::cout << '\n';
}
Output:
1 2 3 4
π Defect reports π
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
- DR
P2259R1β applied to C++20; behavior as published: post increment might discard its result in more situations; correct behavior: a proxy class is used to keep the result. - DR
LWG 3546β applied to C++20; behavior as published: initialization of the proxy object was sometimes ill-formed; correct behavior: situation and definition adjusted. - DR
LWG 3574β applied to C++20; behavior as published: variant was fully constexpr (P2231R1) but common_iterator was not; correct behavior: also made constexpr. - DR
LWG 3595β applied to C++20; behavior as published: functions of the proxy type lacked constexpr and noexcept; correct behavior: added constexpr and noexcept.
π See also π
operator-computes the distance between two iterator adaptors (function template)
π source/version: http://cppreference.com Β· date: 2024.06.10 Β· page: std::common...:operator++(3) β okay bye bestie, go increment something cute! π«Άβ¨
π¦ 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