Boost logo

Boost :

From: Neal D. Becker (ndbecker2_at_[hidden])
Date: 2004-04-12 13:32:35


According to http://boost.org/libs/iterator/doc/iterator_facade.html#minus

"""
template <class Dr1, class V1, class TC1, class R1, class D1,
          class Dr2, class V2, class TC2, class R2, class D2>
typename enable_if_interoperable<Dr1,Dr2,difference>::type
operator -(iterator_facade<Dr1,V1,TC1,R1,D1> const& lhs,
           iterator_facade<Dr2,V2,TC2,R2,D2> const& rhs);

Return Type:

if is_convertible<Dr2,Dr1>::value

 
then
difference shall be iterator_traits<Dr1>::difference_type.
Otherwise
difference shall be iterator_traits<Dr2>::difference_type

Returns:

if is_convertible<Dr2,Dr1>::value

 
then
-((Dr1 const&)lhs).distance_to((Dr2 const&)rhs).
Otherwise,
((Dr2 const&)rhs).distance_to((Dr1 const&)lhs).
"""

Is the preference for the former case over the latter intentional? This
caused some surprise when I tried to re-implement my
cycle_iterator_adapator (a kind of circular buffer adaptor).

std::copy (b, a, ...)
will do:
  template<typename _RandomAccessIter, typename _OutputIter>
    inline _OutputIter
    __copy(_RandomAccessIter __first, _RandomAccessIter __last,
           _OutputIter __result,
           random_access_iterator_tag)
    {
      typedef typename iterator_traits<_RandomAccessIter>::difference_type
          _Distance;
      for (_Distance __n = __last - __first; __n > 0; --__n) {
        *__result = *__first;
        ++__first;
        ++__result;
      }
      return __result;
    }

As I implemented cycle_iterator_adaptor distance_to, it was expecting that
given iterators [a,b], you probably want a positive distance from a to b,
but the way iterator_adaptors is implemented, it wants a negative distance.
Not hard to workaround, but it seemed odd that the default case was
inverted, and I wondered if that was really intentional.


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk