Boost logo

Boost :

Subject: Re: [boost] shared_ptr<>: why no operator[] ? [SEC=UNCLASSIFIED]
From: Levy, Gabriel (Contractor) (Gabriel.Levy_at_[hidden])
Date: 2011-07-27 05:35:04



The following statement

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

is NOT allocating an array of int, it allocates only 1 int and
initialises it to the value of 42.

To instantiate an array the statement would have to use .... (new
So the doco you refered to IS correct.
Hope this clarifies it...

Gabe Levy
Senior Software Engineer, Contractor, Estrings Pty Ltd

-----Original Message-----
From: boost-bounces_at_[hidden]
[mailto:boost-bounces_at_[hidden]] On Behalf Of dgwsoft
Sent: Wednesday, 27 July 2011 6:37 PM
To: boost_at_[hidden]
Subject: Re: [boost] shared_ptr<>: why no operator[] ?

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
Unsubscribe & other changes:
IMPORTANT: This email remains the property of the Department of Defence
and is subject to the jurisdiction of section 70 of the Crimes Act 1914.
If you have received this email in error, you are requested to contact
the sender and delete the email.

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