Boost logo

Boost :

Subject: Re: [boost] difference_type size()?
From: andrey.semashev_at_[hidden]
Date: 2011-10-19 14:53:38

On Wednesday, October 19, 2011 16:44:25 Olaf van der Spek wrote:
> On Wed, Oct 19, 2011 at 4:22 PM, Jeffrey Lee Hellrung, Jr.
> <jeffrey.hellrung_at_[hidden]> wrote:
> >> Sounds like a very weak reason. Especially since:
> >> If an instance of iterator_range is constructed by a client with two
> >> iterators, the client must ensure that the two iterators delimit a
> >> valid closed-open range [begin,end).
> >>
> >> If that's the rationale, it'd be better to add is_valid() or so.
> >
> > What are the requirements on an iterator's difference_type, exactly?
> I don't know.
> > Specifically, are you even guaranteed that it is always convertible to
> > *some* unsigned integral type, much less specifically std::size_t? And,
> > if so, how would you deduce such an unsigned integral type?
> Good questions. But I don't think I know the answer.
> However, given a valid range, end() - begin() can't be negative. Right?
> Most other size() return an unsigned type. Returning a signed type
> results in signed/unsigned mismatches.

There are some points in the Standard, like this for instance (24.2.1/1):

... For every iterator type X for which equality is defined, there is a
corresponding signed integer type called the difference type of the iterator.

However, that's not really relevant, because there is no requirement that
size() returns the difference type. Obtaining size type from difference type
is quite doable with type traits such as make_unsigned.

Boost list run by bdawes at, gregod at, cpdaniel at, john at