Re: [utility] question on prior(x,n)

From: David Abrahams [mailto:dave@boost-consulting.com]
See counting_iterator for inspiration as to why this might be useful.
Got it, thanks. Perhaps these type requirements should be spelled out in the docs (proposed patch below)? And maybe is_full_iterator_traits in detail/iterators.hpp could be slightly changed and turned into a first-class traits "is_iterator", which would then be used for dispatching next/prior calls to std::advance when applicable? Although this would work, I'm not sure that the added dependencies and complexity are worth it - if possible, I think that dropping the two-argument versions of next and prior would be best. Bjorn [Note: The most interesting change to the doc are the following four lines. <p>The one-argument version of next() and prior() can be used with any type that provides operator++() and operator--(), respectively. The two-argument version of next() and prior() can be used with any type that provides operator+() and operator-(), with the exception of iterator types, which only require operator++() and operator--() for both versions.</p> ] *** utility.htm.orig Wed Feb 25 11:47:07 2004 --- utility.htm Wed Feb 25 11:48:17 2004 *************** *** 41,61 **** T next(T x) { return ++x; } template <class T, class Distance> ! T next(T x, Distance n) ! { ! std::advance(x, n); ! return x; ! } template <class T> T prior(T x) { return --x; } template <class T, class Distance> ! T prior(T x, Distance n) ! { ! std::advance(x, -n); ! return x; ! }</pre> </blockquote> <p>Usage is simple:</p> <blockquote> --- 41,54 ---- T next(T x) { return ++x; } template <class T, class Distance> ! T next(T x, Distance n); template <class T> T prior(T x) { return --x; } template <class T, class Distance> ! T prior(T x, Distance n); ! </pre> </blockquote> <p>Usage is simple:</p> <blockquote> *************** *** 66,71 **** --- 59,68 ---- <p>The distance from the given iterator should be supplied as an absolute value. For example, the iterator four iterators prior to the given iterator <code>p</code> may be obtained by <code>prior(p, 4)</code>.</p> + <p>The one-argument version of next() and prior() can be used with any type that provides + operator++() and operator--(), respectively. The two-argument version of next() and prior() + can be used with any type that provides operator+() and operator-(), with the exception of + iterator types, which only require operator++() and operator--() for both versions.</p> <p>Contributed by <a href="../../people/dave_abrahams.htm">Dave Abrahams</a>. Two-argument versions by Daniel Walker.</p> <h2><a name="Class_noncopyable">Class noncopyable</a></h2> <p>Class <strong>noncopyable</strong> is a base class. Derive your own class
participants (1)
-
Bjorn.Karlsson@readsoft.com