Boost logo

Boost :

From: Howard Hinnant (hinnant_at_[hidden])
Date: 2004-01-21 10:28:53

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). But I'm very hesitant to add that overhead for a
functionality that I suspect would rarely be used. By this time I had
flipped back and forth between ptrdiff_t and size_t at least twice! :-)

I think it boils down to this (but correct me if I'm wrong):

S<T[]> s(new T[3]);

If s is constrained to always point to the beginning of this array
(unless reset to another array), then I think size_t makes more sense.
But if s can iterate into its array then ptrdiff_t is a must.

If we go the latter route, then should we go all the way and make
S<T[]> a fully functioning random access iterator? That might be
useful. But at what point does S<T[]>'s functionality (and overhead)
get so close to vector<T> that they might as well be the same class?

<shrug> These are not rhetorical questions. I think I've confused
myself more while I wrote this. I'm still flipping ptrdiff_t/size_t!


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