boost::shared_ptr vs. auto_ptr

C

ctick

Are there any advantages of using boost::shared_ptr other than auto_ptr from
standard library?
 
A

Alf P. Steinbach

* ctick:
Are there any advantages of using boost::shared_ptr other than auto_ptr from
standard library?

There are a number of advantages.

First, they're two different beasts: std::auto_ptr transfers ownership
so that with some caution you can guarantee that only one pointer points
to a particular object at any time, whereas boost::shared_ptr provides
reference counting so that many pointers can point to the same object.

Second, you cannot put std::auto_ptr's in a standard container, but you
can with boost::shared_ptr.

Third, although the standard specially provides for calling a destructor
on an object of incomplete class, not all compilers support that. This
problem pops up in e.g. the pimpl idiom. With boost::shared_ptr you
replace the direct delete expression in std::auto_ptr with a custom
destroy-function that can be defined where the full definition of the
class is available, side-stepping the issue.

I could list up a fourth and fifth advantage, and perhaps more, but I
think that's enough. The main advantage of std::auto_ptr is that it's
always there and that it's standard. It should therefore be used when
the generality of e.g. boost::shared_ptr is not required.
 
D

David Harmon

On Sat, 19 Jun 2004 20:23:26 GMT in comp.lang.c++, (e-mail address removed) (Alf
P. Steinbach) wrote,
I could list up a fourth and fifth advantage, and perhaps more, but I
think that's enough. The main advantage of std::auto_ptr is that it's
always there and that it's standard. It should therefore be used when
the generality of e.g. boost::shared_ptr is not required.

I think that auto_ptr also has less overhead cost than shared_ptr's
ownership tracking mechanism when you are doing the simple things it is
capable of and have no need for shared ownership.
 
R

Richard Herring

David Harmon said:
On Sat, 19 Jun 2004 20:23:26 GMT in comp.lang.c++, (e-mail address removed) (Alf
P. Steinbach) wrote,

I think that auto_ptr also has less overhead cost than shared_ptr's
ownership tracking mechanism when you are doing the simple things it is
capable of and have no need for shared ownership.
For the _really_ simple things, consider boost::scoped_ptr. No
overheads, no sharing or transfer of ownership at all.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top