operator==,!=,<,<=,>,>=,<=>(std::reverse_iterator)

Header: <iterator>

Compares the underlying iterators of lhs and rhs.

# Declarations

template< class Iter1, class Iter2 >
bool operator==( const std::reverse_iterator<Iter1>& lhs,
const std::reverse_iterator<Iter2>& rhs );

(constexpr since C++17)

template< class Iter1, class Iter2 >
bool operator!=( const std::reverse_iterator<Iter1>& lhs,
const std::reverse_iterator<Iter2>& rhs );

(constexpr since C++17)

template< class Iter1, class Iter2 >
bool operator< ( const std::reverse_iterator<Iter1>& lhs,
const std::reverse_iterator<Iter2>& rhs );

(constexpr since C++17)

template< class Iter1, class Iter2 >
bool operator<=( const std::reverse_iterator<Iter1>& lhs,
const std::reverse_iterator<Iter2>& rhs );

(constexpr since C++17)

template< class Iter1, class Iter2 >
bool operator> ( const std::reverse_iterator<Iter1>& lhs,
const std::reverse_iterator<Iter2>& rhs );

(constexpr since C++17)

template< class Iter1, class Iter2 >
bool operator>=( const std::reverse_iterator<Iter1>& lhs,
const std::reverse_iterator<Iter2>& rhs );

(constexpr since C++17)

template< class Iter1, std::three_way_comparable_with<Iter1> Iter2 >
constexpr std::compare_three_way_result_t<Iter1, Iter2>
operator<=>( const std::reverse_iterator<Iter1>& lhs,
const std::reverse_iterator<Iter2>& rhs );

(since C++20)

# Parameters

# Notes

operator<=> returns rhs.base() <=> lhs.base() rather than lhs.base() <=> rhs.base() because this is a reverse iterator.

# Example

#include <cassert>
#include <iterator>
 
int main()
{
    int a[]{0, 1, 2, 3};
    //            ↑  └───── x, y
    //            └──────── z
    // “x” and “y” are equal, but “x” is less than “z” (reversely)
    std::reverse_iterator<int*>
        x{std::rend(a) - std::size(a)},
        y{std::rend(a) - std::size(a)},
        z{std::rbegin(a) + 1};
 
    // two-way comparisons
    assert(  x == y );
    assert(!(x != y));
    assert(!(x <  y));
    assert(  x <= y );
    assert(!(x == z));
    assert(  x != z );
    assert(  x <  z );
    assert(  x <= z );
 
    // three-way comparisons
    assert(  x <=> y == 0 );
    assert(!(x <=> y <  0));
    assert(!(x <=> y >  0));
    assert(!(x <=> z == 0));
    assert(  x <=> z <  0 );
    assert(!(x <=> z >  0));
}

# Defect reports

DRApplied toBehavior as publishedCorrect behavior
LWG 280C++98heterogeneous assignment was not allowedallowed