Boost logo

Boost :

From: jsiek_at_[hidden]
Date: 1999-11-28 19:17:22


Dave Abrahams writes:
> Except that if you don't have a random access iterator unless op- is
> supported. Whether an algorithm chooses to make use of op- is an
> implementation detail. If you give an iterator's traits the tag
> random_access_iterator_tag, but you don't define op-, you're essentially
> lying to whatever library is going to use the traits.

True, if we define the random_access_iterator_tag, the iterator
should be a RandomAccessIterator. But on the other hand, the
random_access_iterator_tag was added only as a convenience to the
user. In general I am against adding extra requirements that aren't
really needed (it is a barrier to reuse). However, in this case I see
little harm in requiring the difference operator so that we can go
ahead an define the random_access_iterator_tag.

> That would be an improvement, but only inasmuch as it improves the error
> messages you get. As it stands, if you try to derive from
> less_than_comparable and you haven't defined op<, your program is ill-formed
> and you WILL get a diagnostic.

True.

> One possible solution to my problem: have the random access helper define a
> friend function (never called) which calls op- on a pair of const T&, and
> somehow ensures that the result has the appropriate difference_type. I'm
> sure we can come up with an answer to that last missing detail.

This would do the trick:

template <class T,
          class V,
          class D = std::iterator_traits<char*>::difference_type,
          class P = V*,
          class R = V&>
struct random_access_iterator_helper :
  equality_comparable<T,T>,
  less_than_comparable<T,T>,
  incrementable<T>,
  decrementable<T>,
  dereferenceable<T,V>,
  addable<T,D>,
  subtractable<T,D>,
  indexable<T,D,R>,
  std::iterator<std::random_access_iterator_tag, V, D, P, R>
{
  friend D requires_difference_operator(const T& x, const T& y) {
    return x - y;
  }
};

Cheers,

Jeremy


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