Boost logo

Boost :

From: Daniel Frey (daniel.frey_at_[hidden])
Date: 2004-02-23 05:57:55


Gabriel Dos Reis wrote:
> David Abrahams <dave_at_[hidden]> writes:
>
> | Daniel Frey <daniel.frey_at_[hidden]> writes:
> |
> | > Consider:
> | >
> | > // A strange way to retrieve my_container.begin():
> | > boost::prior( my_container.end(), my_container.size() );
> | >
> | > Is that legal? The second template parameter of boost::prior is
> | > deduced to an unsigned(!) type, which IMHO means that -n is undefined,
> | > right? Do we need to fix this? Maybe:
> | >
> | > template <class T>
> | > T prior(T x, typename T::difference_type n)
> | > {
> | > std::advance(x, -n);
> | > return x;
> | > }
> |
> | Well, that wouldn't work!
> | How would you call prior(p, 4) when p is int*?
>
> I suppose he wanted to say
>
> template<class T>
> T prior(T x, typename std::iterator_traits<T>::different_type n)
>
> ?

You are obviously correct, thank you (But it's still difference_type,
not different_type).

> | > or shall we document that it's illegal/undefined to call
> | > boost::prior() with an unsigned second parameter?
> |
> | Hmm, no I think if it's unsigned we should either deduce an
> | appropriate signed type or implement the "backward advance"
> | ourselves. We could use reverse_iterator to do that, FWIW.

Should we propose std::regress (the counter-part for std::advance) for
standardization (via a DR)? The current definition of std::advance seems
to limit the number of elements that can be skipped backwards to signed
types, but the container can be larger (OK, in theory, never tried it
myself :). This would also nicely solve the implementation issue for us.

Since then, the reverse_iterator implementation would be this:

template <class T, class Distance>
T prior(const T& x, Distance n)
{
    return boost::next( std::reverse_iterator<T>(x), n ).base();
}

right? Although I wonder what happens if T is already a reverse
iterator. Is it legal to reverse a reverse_iterator? Or do we need a
trait and implement both cases separately?

Regards, Daniel

-- 
Daniel Frey
aixigo AG - financial solutions & technology
Schloß-Rahe-Straße 15, 52072 Aachen, Germany
fon: +49 (0)241 936737-42, fax: +49 (0)241 936737-99
eMail: daniel.frey_at_[hidden], web: http://www.aixigo.de

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