std::revers...::operator=(3)
π 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: astd::reverse_iteratorcould be constructed, but not assigned, from anotherstd::reverse_iteratorwith 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
- 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