
On Wed, 15 Mar 2006 14:12:07 -0000, "John Maddock" <john@johnmaddock.co.uk> wrote:
I have detected that the standard declares "distance" template at 24.3.4, as taking two "InputIterator" as parameters. <snip>
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).
<...>
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....
Is it really a bug? AFAIK, the compiler is ordered to apply template function "distance" on two members of the class. The function is const, so the members must be const, in this case its final type will be "const char * const". So the template deduced automatically will have the type "const char * const" as "InputIterator", which is not a valid input iterator, as it may not step forward or backward. The compiler must be broken anyway, because changing the distance call to the explicit distance<iterator>(...), where iterator is the type defined within the struct (and thus, it should be "const char *") will not work either. Zara