Boost logo

Boost :

From: John Max Skaller (skaller_at_[hidden])
Date: 2001-08-20 17:02:26


Kevlin Henney wrote:

> > I agree. My main contention is that
> >
> > a) we should drop shared_array
> > b) we should use
> >
> > shared_ptr< array<T> >
> > shared_ptr< fixed_array<T,n> >
> >
> >instead of
> >
> > shared_ptr< T[] >
> > shared_ptr< T[n] >
> [...]
> >I.e.: I agree with removing shared_array and using shared_ptr,
> >but suggest that instead of a specialisation, to use first class
> >array types.
 
> This seems to be a better idea in theory than in practice:

        There is no such thing. Theory is Always Right,
Practice is RArely Good. TARPRAG principle. (TM) Max, 19XX, 20XX:-)
 
> shared_ptr< array<T> > p;
> ...
> (*p)[i] = ...;
>
> Whereas a shared (or scoped) array that was specialised as such would
> support operator[] directly.

        Exactly. Which is why the specialisation is a seriously
bad idea: this usage is unsafe and can't be generalised.

        The correct way to use array<T> is

        array<int> x(3); ...

        array<int>::iterator p = x.begin();
        p[1]=p[2]; // subscript the ITERATORS not the array

> So, dropping the idea of a separate shared array implementation is not
> really appropriate. It becomes a question of whether to support it as a
> specialisation or as a separately named class template.

        No. You aren't being logical. The correct conclusion
is that shared pointers denote containers, of which array<T>
is just one example. If you used a vector:

        shared_ptr<vector<T>> x;
        x[i] // error!!

you'd EXPECT an error. So you should EXPECT an error
for an array container too -- after all a vector is a
kind of array.

If you REALLY want a 'specialisation', it should work for
ALL containers with a canonical random access iterator.

Which means that providing operator[] for shared pointer
is CORRECT. The fact that this is unsafe using
C arrays is irrelevant. They're unsafe anyhow.
Just don't use them.

-- 
John (Max) Skaller, mailto:skaller_at_[hidden] 
10/1 Toxteth Rd Glebe NSW 2037 Australia voice: 61-2-9660-0850
New generation programming language Felix  http://felix.sourceforge.net
Literate Programming tool Interscript     
http://Interscript.sourceforge.net

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