std::ranges...r++,--,+=,-=(3)
π NAME π
okay so bestie, this is the std::ranges...r++,--,+=,-=(3) page from the C++ Standard Libary β yes, the header really says βLibaryβ, and honestly? iconic typo. π
The NAME says: std::ranges::adjacent_view::iterator::operator++,--,+=,-= - std::ranges::adjacent_view::iterator::operator++,--,+=,-= β yep, it says it twice, and weβre keeping both because rules are rules. β¨
This is the whole little family of iterator moves for adjacent_viewβs iterator, and itβs giving C++23 from the very first line. π¦
β¨ Synopsis β¨
constexpr /*iterator*/& operator++(); (1) (since C++23)
constexpr /*iterator*/ operator++( int ); (2) (since C++23)
constexpr /*iterator*/& operator--() (3) (since C++23)
requires ranges::bidirectional_range<Base>;
constexpr /*iterator*/ operator--( int ) (4) (since C++23)
requires ranges::bidirectional_range<Base>;
constexpr /*iterator*/& operator+=( difference_type n ) (5) (since C++23)
requires ranges::random_access_range<Base>;
constexpr /*iterator*/& operator-=( difference_type n ) (6) (since C++23)
requires ranges::random_access_range<Base>;
Increments or decrements the iterator. Let current_ be an underlying array of iterators. π«
1) β Equivalent to:
for (auto& i : current_)
i = std::ranges::next(i);
return *this;
β οΈ undefined behavior alert: the behavior is undefined if before the call current_.back() is not incrementable. Like, do not test fate, bestie. π
2) β Equivalent to:
auto tmp = *this;
++*this;
return tmp;
3) β Equivalent to:
for (auto& i : current_)
i = std::ranges::prev(i);
return *this;
β οΈ undefined behavior alert: the behavior is undefined if before the call current_.front() is not decrementable. Again: not the vibe. π
4) β Equivalent to:
auto tmp = *this;
--*this;
return tmp;
5) β Equivalent to:
for (auto& i : current_)
i = i + n;
return *this;
β οΈ undefined behavior alert: the behavior is undefined if before the call current_.back() + n does not have well-defined behavior. Read that twice, bestie. π
6) β Equivalent to:
for (auto& i : current_)
i = i - n;
return *this;
β οΈ undefined behavior alert: the behavior is undefined if before the call current_.front() - n does not have well-defined behavior. periodt. π₯
πΈ Parameters πΈ
nβ position relative to current location
π Return value π
- 1, 3, 5, 6)
*this - 2, 4) A copy of
*thisthat was made before the change.
π§ Example π§
// Run this code
#include <cassert>
#include <list>
#include <ranges>
#include <utility>
#include <vector>
[[nodiscard]]
bool operator==(std::pair<int&, int&> x, std::pair<int, int> y)
{
return x.first == y.first and x.second == y.second;
}
int main()
{
{
auto v = std::vector{0, 1, 2, 3, 4, 5};
auto i = (v | std::views::pairwise).begin();
assert((*i == std::pair{0, 1}));
++i; // overload (1)
assert((*i == std::pair{1, 2}));
--i; // overload (3)
assert((*i == std::pair{0, 1}));
i += 2; // overload (5)
assert((*i == std::pair{2, 3}));
i -= 2; // overload (6)
assert((*i == std::pair{0, 1}));
}
{
auto v = std::list{0, 1, 2, 3, 4, 5};
auto i = (v | std::views::pairwise).begin();
assert((*i == std::pair{0, 1}));
++i; // overload (1)
assert((*i == std::pair{1, 2}));
--i; // overload (3)
assert((*i == std::pair{0, 1}));
// i += 2; // Error: v is not a random_access_range; overload (5)
// i -= 2; // Error: v is not a random_access_range; overload (6)
}
}
π₯ See also π₯
operator+performs iterator arithmeticoperator-(public member function)(C++23)
okay thatβs the whole page, bestie π«Ά β source: http://cppreference.com, version/date: 2024.06.10, manual: std::ranges...r++,--,+=,-=(3). go forth and increment responsibly. πβ¨
π¦ 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