Boost logo

Boost :

Subject: Re: [boost] difference_type size()?
From: andrey.semashev_at_[hidden]
Date: 2011-10-19 15:09:17


On Wednesday, October 19, 2011 11:50:06 Jeffrey Lee Hellrung, Jr. wrote:
> > Making the size signed makes little sense to me and it also
> > introduces a problem (rather theoretical, however) when iterator_range
> > is constructed from
> > a very large valid range, whose size exceeds
> > numeric_limits<difference_type>::max().
>
> How would that be possible? Doesn't that violate the conditions on
> difference_type that it is able to represent the (signed) distance between
> any two iterators within the same range?

That is only true for random access iterators. See std::distance reference
(24.4.4/4):

Effects: If InputIterator meets the requirements of random access iterator,
returns (last - first); otherwise, returns the number of increments needed to
get from first to last.

So bidirectional iterators, for instance, may denote a valid range that
exceeds difference_type capacity. This is actually a little caveat in the
Standard.

> Also, assuming that size() should return an unsigned integer type (I'm not
> debating the merits of this), *what* unsigned integer type should that be?
> I don't know of any general way to go from difference_type to a "compatible"
> unsigned integer type. I suspect this will end up being filed away as
> "nice to have but ultimately unimplementable generically" given current
> range interfaces.

I think the following would do:

  typedef typename make_unsigned< difference_type >::type size_type;


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