shared_ptr<>: why no operator[] ?

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 -- View this message in context: http://boost.2283326.n4.nabble.com/shared-ptr-why-no-operator-tp3697208p3697... Sent from the Boost - Dev mailing list archive at Nabble.com.

On Tue, Jul 26, 2011 at 11:39 PM, dgwsoft <gareth@dgwsoft.co.uk> 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

Neil Groves-3 wrote:
On Tue, Jul 26, 2011 at 11:39 PM, dgwsoft <gareth@dgwsoft.co.uk> 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-tp3697208p3697... Sent from the Boost - Dev mailing list archive at Nabble.com.

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: boost-bounces@lists.boost.org [mailto:boost-bounces@lists.boost.org] On Behalf Of dgwsoft Sent: Wednesday, 27 July 2011 6:37 PM To: boost@lists.boost.org Subject: Re: [boost] shared_ptr<>: why no operator[] ? Neil Groves-3 wrote:
On Tue, Jul 26, 2011 at 11:39 PM, dgwsoft <gareth@dgwsoft.co.uk> 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.

yes of course, Julien and Gaby you are right, I was misreading it. Confusion with std::vector<int> p(42), I think. cheers Gareth -- View this message in context: http://boost.2283326.n4.nabble.com/shared-ptr-why-no-operator-tp3697208p3698... Sent from the Boost - Dev mailing list archive at Nabble.com.
participants (4)
-
dgwsoft
-
Julien Nitard
-
Levy, Gabriel (Contractor)
-
Neil Groves