Boost logo

Boost :

Subject: Re: [boost] [histogram] should some_axis::size() return unsigned or int?
From: Gavin Lambert (boost_at_[hidden])
Date: 2018-12-03 05:18:18


On 3/12/2018 15:15, Jorg Brown wrote:
> "distance(begin, end)" isn't O(1) for many types of containers, which is
> why P1227 defines ssize() as the static_casted result of size().
>
> Of course, for many containers, size() is implemented as the subtraction of
> two pointers, static_casted to size_t. (See
> https://github.com/llvm-mirror/libcxx/blob/master/include/vector for
> example) In those cases, it would make more sense to define ssize() as
> simply the subtraction of those two pointers.

Which is why I added the "moral equivalent" clause. :)

> ""better" in that it allows a container intended exclusively for small
> sizes to use a difference_type smaller than ptrdiff_t."
>
> I applaud your use of quotation marks around better, since such a
> difference_type is often inefficient in that the caller is often forced to
> immediately promote it to larger size, before the processor can work with
> it.

True. But it also in theory allows for more esoteric indexing, such as
indexing by an enum or a complex value or some other UDT. (Technically
that doesn't meet the requirements of Container, but provided the type
behaves sufficiently like an integer then most algorithms would still work.)

Whether that's "better" or not might be another argument. :)

And AFAIK all the standard container types use ptrdiff_t as their
difference_type anyway, so it seems reasonable to use that instead of
mandating ptrdiff_t.

> I'm curious about whether you've considered using 0 and size() - 1 as the
> indices for the special bins?

IIRC, Hans stated that another library does this and it makes it more
awkward to use, since (he says) mostly you ignore the extra bins and
turning them on or off changes the indexing of the regular bins.


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