[Libpqxx-general] distance() return type

Jeroen Vermeulen jtv at xs4all.nl
Fri Jul 18 08:15:17 UTC 2008


Jorgen Austvik - Sun Norway wrote:
> Hi,
> 
> in include/pqxx/compiler-internal.hxx, the distance function is defined 
> like this:
> 
> ----8<-----------8<-----------8<-----------8<-----------8<-----------8<-------
> /// Wrapper for std::distance; not all platforms have std::distance().
> template<typename T> inline ptrdiff_t distance(T first, T last)
> {
> #ifdef PQXX_HAVE_DISTANCE
>    return PGSTD::distance(first, last);
> #else
>    // Naive implementation.  All we really need for now.
>    iterator_traits<T>::difference_type d = 0;
>    while (first != last) ++d;
>    return d;
> #endif
> }
> ----8<-----------8<-----------8<-----------8<-----------8<-----------8<-------
> 
> I am curious why the return type of the function is ptrdiff_t while it 
> looks like std::distance returns iterator_traits<T>::difference_type[1], 
> and the d variable certainly has that type - should they all be 
> iterator_traits<T>::difference_type?
> 
> On the Sun Studio compiler, with it's Rough Wave STL implementation, 
> iterator_traits does not exist (and std::distance is also non-standard). 
> There I just change iterator_traits<T>::difference_type d = 0 to 
> ptrdiff_t d = 0, which seems to do the job there.

This was done exactly for the Sun Studio compiler, and indeed this code 
seemed to work on the version I've had access to.  But now that you 
mention it, I don't see how the workaround can work at all!

I've fixed that, and made d a ptrdiff_t.  See checkin [1350].


With thanks,

Jeroen


More information about the Libpqxx-general mailing list