std::common...:operator++(3)

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

🌸 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 underlying std::variant member object var does not hold an object of type I, i.e. std::holds_alternative<I>(var) is equal to false. 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).

  1. Pre-increments by one. Equivalent to:
++it; return *this;
  1. 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> or
  • std::constructible_from<std::iter_value_t<I>, std::iter_reference_t<I>> or
  • std::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 🌈

  1. *this
  2. A copy of *this that 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