std::revers...::operator=(3)

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

πŸ’– NAME πŸ’–

std::reverse_iterator::operator= β€” okay bestie, this is the assignment operator for std::reverse_iterator, straight from the C++ Standard Libary manual, section 3. It’s giving β€œcopy the underlying iterator energy” and honestly? Iconic. ✨


✨ Synopsis ✨

template< class U >
reverse_iterator& operator=( const reverse_iterator<U>& other );

until C++17

template< class U >
constexpr reverse_iterator& operator=( const reverse_iterator<U>& other );

since C++17

The underlying iterator gets assigned the value of the underlying iterator of other, i.e. other.base(). No cap.

This overload participates in overload resolution only if U is not the same type as Iter and std::convertible_to<const U&, Iter> and (since C++20) std::assignable_from<Iter&, const U&> are modeled. That’s the whole vibe check. 🫢

πŸŽ€ Parameters πŸŽ€

  • other β€” iterator adaptor to assign. That’s the bestie you’re copying from. πŸ’…

πŸ¦‹ Return value πŸ¦‹

*this β€” yes, it returns itself. Main character behavior, honestly. πŸ‘‘

🌸 Example 🌸

// Run this code

#include <iostream>
#include <iterator>

int main()
{
    const int a1[]{0, 1, 2};
    int a2[]{0, 1, 2, 3};
    short a3[]{40, 41, 42};

    std::reverse_iterator<const int*> it1{std::crbegin(a1)};
    it1 = std::reverse_iterator<int*>{std::rbegin(a2)};   // OK
//  it1 = std::reverse_iterator<short*>{std::rbegin(a3)}; // compilation error:
                                                           // incompatible pointer types
    std::reverse_iterator<short const*> it2{nullptr};
    it2 = std::rbegin(a3); // OK
//  it2 = std::begin(a3);  // compilation error: no viable overloaded '='
    std::cout << *it2 << '\n';
}

🌈 Output 🌈

42

πŸ’… Defect reports πŸ’…

The following behavior-changing defect reports were applied retroactively to previously published C++ standards. So yes, the lore goes back. 😭

  • DR LWG 280 β€” applied to C++98. Behavior as published: a std::reverse_iterator could be constructed, but not assigned, from another std::reverse_iterator with a different underlying iterator type. Correct behavior: also allowed assignment. Slay.
  • DR LWG 3435 β€” applied to C++20. Behavior as published: the converting assignment operator was not constrained. Correct behavior: constrained. Periodt.

πŸ‘‘ See also πŸ‘‘

  • constructor β€” constructs a new iterator adaptor (public member function)

fr fr, that’s the whole page 🫢 β€” sourced from http://cppreference.com, version 2024.06.10, page std::revers...::operator=(3). mwah πŸ’‹

πŸ“¦ where this page comes from