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:

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



View this message in context:
Sent from the Boost - Dev mailing list archive at

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