[smart_ptr] No implicit conversion of shared_array

Hi why doesn't shared_array allow the same conversions as shared_ptr, so that boost::shared_ptr<int> ip; boost::shared_ptr<const int> ip2(ip); is allowed, but not boost::shared_array<int> ip; boost::shared_array<const int> ip2(ip); Is it safe to use shared_ptr's with arrays if one supplies an array deleter as a constructor argument? Thanks in advance for any hint. Regards, Stephan

On 9/5/06, Stephan Tolksdorf <andorxor@gmx.de> wrote:
Is it safe to use shared_ptr's with arrays if one supplies an array deleter as a constructor argument?
How about a shared_ptr< vector<int> > or something? Phillip

Is it safe to use shared_ptr's with arrays if one supplies an array deleter as a constructor argument?
How about a shared_ptr< vector<int> > or something?
I'd like to avoid the vector solution and am still curious whether the shared_ptr + array-deleter is safe... Stephan

Stephan Tolksdorf wrote:
Hi
why doesn't shared_array allow the same conversions as shared_ptr, so that
boost::shared_ptr<int> ip; boost::shared_ptr<const int> ip2(ip);
is allowed, but not
boost::shared_array<int> ip; boost::shared_array<const int> ip2(ip);
shared_array doesn't support conversions since shared_array<Derived> should not be convertible to shared_array<Base>, even though Derived* is convertible to Base*. We might be able to add the limited cv-conversions using enable_if tricks.
Is it safe to use shared_ptr's with arrays if one supplies an array deleter as a constructor argument?
Yes, it is safe, but the above caveat still applies. http://www.boost.org/libs/smart_ptr/sp_techniques.html#array

shared_array doesn't support conversions since shared_array<Derived> should not be convertible to shared_array<Base>, even though Derived* is convertible to Base*. We might be able to add the limited cv-conversions using enable_if tricks.
Thanks a lot for the explanation. Enabling the cv-conversions would definitely increase the uselfulness of shared_array. Stephan

Stephan Tolksdorf <andorxor@gmx.de> writes:
Hi
why doesn't shared_array allow the same conversions as shared_ptr, so that
boost::shared_ptr<int> ip; boost::shared_ptr<const int> ip2(ip);
is allowed, but not
boost::shared_array<int> ip; boost::shared_array<const int> ip2(ip);
Probably because that particular case wasn't anticipated. It was deemed important to prevent this: struct B { int x; }; struct D : B { int y; }; boost::shared_array<D> p1(new D[20]); boost::shared_array<B> p2(p1); p2[1]; // oops!
Is it safe to use shared_ptr's with arrays if one supplies an array deleter as a constructor argument?
Yeah, if you consider allowing my oops example to be "safe"... -- Dave Abrahams Boost Consulting www.boost-consulting.com
participants (4)
-
David Abrahams
-
Peter Dimov
-
Phillip Hellewell
-
Stephan Tolksdorf