operator-(std::counted_iterator)

Computes the distance between two iterator adaptors.

# Declarations

template< std::common_with<I> I2 >
friend constexpr std::iter_difference_t<I2> operator-(
const counted_iterator& x, const counted_iterator<I2>& y );

(since C++20)

# Parameters

# Return value

y.count() - x.count()

# Notes

Since the length counts down, not up, the order of the arguments of operator- in the underlying expression is reversed, i.e. y is lhs and x is rhs.

# Example

#include <initializer_list>
#include <iterator>
 
int main()
{
    static constexpr auto v = {1, 2, 3, 4, 5, 6};
    constexpr std::counted_iterator<std::initializer_list<int>::iterator>
        it1{v.begin(), 5},
        it2{it1 + 3},
        it3{v.begin(), 2};
 
    static_assert(it1 - it2 == -3);
    static_assert(it2 - it1 == +3);
//  static_assert(it1 - it3 == -3); // UB: operands of operator- do not refer to
                                    // elements of the same sequence
}

# See also