Boost logo

Boost :

Subject: Re: [boost] shared_ptr<>: why no operator[] ?
From: dgwsoft (gareth_at_[hidden])
Date: 2011-07-27 05:07:20


Neil Groves-3 wrote:
>
> On Tue, Jul 26, 2011 at 11:39 PM, dgwsoft &lt;gareth_at_[hidden]&gt;
> wrote:
>
>> boost::shared_ptr<int> p(new int(42));
>>
>> To access elements of the array I can do:
>>
>> p.get()[10] = 7;
>>
>> But wouldn't it be nicer to do:
>>
>> p[10] = 7; // ?
>>
>> So: why is no T& operator[](int) defined for shared_ptr ? I'm just
>> interested in the design reasons for this.
>>
>>
> This isn't guaranteed to properly destroy the array. It will call delete,
> rather than delete[].
> This is why there is also boost::shared_array which is what you should be
> using and has an operator[].
>

Thanks, Neil, that makes perfect sense. One should never do:

boost::shared_ptr<int> p(new int(42));

In which case, someone should look at this page:
http://www.boost.org/doc/libs/1_47_0/libs/smart_ptr/shared_ptr.htm#example

(under Thread Safety) which is where I copied that code from.

cheers

Gareth

--
View this message in context: http://boost.2283326.n4.nabble.com/shared-ptr-why-no-operator-tp3697208p3697938.html
Sent from the Boost - Dev mailing list archive at Nabble.com.

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