shared_ptr<>: why no operator[] ?

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

shared_ptr<>: why no operator[] ?

dgwsoft
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.

cheers

Gareth
Reply | Threaded
Open this post in threaded view
|

Re: shared_ptr<>: why no operator[] ?

Neil Groves-3
On Tue, Jul 26, 2011 at 11:39 PM, dgwsoft <[hidden email]> 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[].
 I hope this helps.

Regards,
Neil Groves
_______________________________________________
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Reply | Threaded
Open this post in threaded view
|

Re: shared_ptr<>: why no operator[] ?

dgwsoft
Neil Groves-3 wrote
On Tue, Jul 26, 2011 at 11:39 PM, dgwsoft <[hidden email]> 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


Reply | Threaded
Open this post in threaded view
|

Re: shared_ptr<>: why no operator[] ?

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

Correct me if I am wrong but this is perfectly valid. It allocates a single
int and assigns it the value 42. It doesn't create an array.

regards,

Julien
_______________________________________________
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Reply | Threaded
Open this post in threaded view
|

Re: shared_ptr<>: why no operator[] ? [SEC=UNCLASSIFIED]

Levy, Gabriel (Contractor)
In reply to this post by dgwsoft
UNCLASSIFIED

Note!!!

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
int[42])
 
So the doco you refered to IS correct.
Hope this clarifies it...



Gabe Levy
Senior Software Engineer, Contractor, Estrings Pty Ltd

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


Neil Groves-3 wrote:

>
> On Tue, Jul 26, 2011 at 11:39 PM, dgwsoft &lt;[hidden email]&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#examp
le

(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-tp3697208p
3697938.html
Sent from the Boost - Dev mailing list archive at Nabble.com.
_______________________________________________
Unsubscribe & other changes:
http://lists.boost.org/mailman/listinfo.cgi/boost

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.

_______________________________________________
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Reply | Threaded
Open this post in threaded view
|

Re: shared_ptr<>: why no operator[] ? [SEC=UNCLASSIFIED]

dgwsoft
yes of course, Julien and Gaby you are right, I was misreading it. Confusion with std::vector<int> p(42), I think.

cheers

Gareth