This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [Bug libstdc++/29914] std::distance(iterator, const_iterator)


> 
> 
> 
> ------- Comment #2 from wouter dot vermaelen at pi dot be  2006-11-20 22:47 -------
> I don't know of any implementation that does accept this. It was just very
> unexpected and inconvenient that it didn't work.
> 
> I don't know C++ well enough, so maybe the following is completely flawed. But
> wouldn't it be possible to extend iterator_traits with a const_iterator typedef
> and provide an overload for distance() like this:
> 
> template <typename IT1, typename IT2>
> typename std::iterator_traits<IT1>::difference_type distance(IT1 it1, IT2 it2)
> {
>         typedef typename std::iterator_traits<IT1>::const_iterator CIT1;
>         typedef typename std::iterator_traits<IT2>::const_iterator CIT2;
>         CIT1 cit1 = it1;
>         CIT2 cit2 = it2;
>         return std::distance(cit1, cit2);
> }

That would cause confusion in cases where distance is passed vert different iterators.
Maybe something more like:

template <typename IT1>
typename std::iterator_traits<IT1>::difference_type distance(IT1 it1,
	typename std::iterator_traits<IT1>::const_iterator it2)
{
  typename std::iterator_traits<IT1>::const_iterator  cit1 = it1;
  return std::distance(cit1, it2);
}

And one for the const iterator in the first argument.

-- Pinski


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]