Boost logo

Boost :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2004-01-21 10:41:48


Howard Hinnant wrote:
> On Jan 21, 2004, at 8:21 AM, Peter Dimov wrote:
>
>> Howard Hinnant wrote:
>>>
>>> Therefore an interface should not imply that you can handle arrays
>>> of objects by simply customizing the deletion policy. Smart
>>> pointers to arrays of objects require a different interface,
>>> including:
>>>
>>> * no dervied-to-base conversions
>>> * no operator*()
>>> * no operator->()
>>> * operator[](size_t)
>>
>> operator[](ptrdiff_t) please. :-)
>
> <chuckle> When I was writing that list I must've switched back and
> forth between ptrdiff_t and size_t three times! :-)
>
> ptrdiff_t is the "natural" type for indexing a raw C array, and that's
> why I put ptrdiff_t down first. But then I got to thinking about:
>
> S<T[]> s;
> ...
> s[-1] = ...
>
> How would such a situation arise and what would it mean? Then it
> occurred to me that a major difference between s, and a pointer to an
> array (say T* p) is that s /must/ point to the beginning of an array,
> whereas p can be incremented into the array and thus p[-1] makes
> sense. If we modify s's interface such that it too can be
> incremented into
> the array, then we have to add data members to keep track of the
> beginning of the array so that we can delete it (and then s[-1] makes
> sense too).

Yes, shared_array can be extended that way. But my point was much simpler:
you can assert(i >= 0) in operator[](ptrdiff_t i) to catch s[-1], but you
can't if i is size_t, and s[-1] still works. :-)

(Of course you can assert(i < SIZE_T_MAX / 2) but that's just dancing
around.)

The only reason size_t works for vector is that vector knows its size(), so
it can assert(i < size()) and catch negative indices for free.


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