Boost logo

Boost :

From: John Maddock (john_at_[hidden])
Date: 2006-03-15 09:12:07


> I have detected that the standard declares "distance" template at
> 24.3.4, as taking two "InputIterator" as parameters.
>
> IMHO, the parameters shoudl be "const InputIterator" as they should
> not be changed by that function. In fact, the version I have seen of
> STLPORT does implement it like that ("const InputIterator") violating
> the standard, while DINKUMWARE respects the standard defintion.

It makes no difference: the function distance takes it's parameters by value
so the two forms:

template <class InputIterator>
typename iterator_traits<InputIterator>::difference_type
distance(InputIterator first, InputIterator last);

and

template <class InputIterator>
typename iterator_traits<InputIterator>::difference_type
distance(InputIterator const first, InputIterator const last);

are actually exactly the same function (the const declaration only affects
the mutability of the arguments within the scope of the function, they have
no effect on the function's type).

> This has given me some problems when trying to use BOOST libraries
> with BDS 2006 (which comes with Dinkumware library).
>
> The function "size" in boost::iterator_range is declared "const", so
> that when it calls "std::distance", it will do so with "const
> IteratorT" parameters. With STLPORT, there will be no problem, as the
> undelying iterato_traits do not change.
>
> But in the case of DINKUMWARE, the underlying iterator_traits change.
> This is specially evident when IteratorT is [const] char *, which is a
> valid random access iterator. "size" will call "std::distance" with
> "[const] char * const" parameters, which are non-valid random
> iterators.

That's unfortunately a compiler bug: the compiler is deducing the template
parameters for distance as "T const" rather than "T", I've worked around
this in the past by explicitly casting away the constantness of the
arguments to distance, but it's a pain to get right all over the place, just
to keep one broken compiler happy....

John.


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