Connecting Tech Pros Worldwide Forums | Help | Site Map

boost::shared_ptr vs. auto_ptr

ctick
Guest
 
Posts: n/a
#1: Jul 22 '05
Are there any advantages of using boost::shared_ptr other than auto_ptr from
standard library?




Alf P. Steinbach
Guest
 
Posts: n/a
#2: Jul 22 '05

re: boost::shared_ptr vs. auto_ptr


* ctick:[color=blue]
> Are there any advantages of using boost::shared_ptr other than auto_ptr from
> standard library?[/color]

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.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
David Harmon
Guest
 
Posts: n/a
#3: Jul 22 '05

re: boost::shared_ptr vs. auto_ptr


On Sat, 19 Jun 2004 20:23:26 GMT in comp.lang.c++, alfps@start.no (Alf
P. Steinbach) wrote,[color=blue]
>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.[/color]

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.

Siemel Naran
Guest
 
Posts: n/a
#4: Jul 22 '05

re: boost::shared_ptr vs. auto_ptr


"ctick" <ctick@flare.com> wrote in message
news:g61Bc.6352$OB3.4344@bgtnsc05-
[color=blue]
> Are there any advantages of using boost::shared_ptr other than auto_ptr[/color]
from[color=blue]
> standard library?[/color]

You're comparing apples to oranges.


Julie
Guest
 
Posts: n/a
#5: Jul 22 '05

re: boost::shared_ptr vs. auto_ptr


Siemel Naran wrote:[color=blue]
>
> "ctick" <ctick@flare.com> wrote in message
> news:g61Bc.6352$OB3.4344@bgtnsc05-
>[color=green]
> > Are there any advantages of using boost::shared_ptr other than auto_ptr[/color]
> from[color=green]
> > standard library?[/color]
>
> You're comparing apples to oranges.[/color]

http://www.people.virginia.edu/~rjh9u/apporang.html
Richard Herring
Guest
 
Posts: n/a
#6: Jul 22 '05

re: boost::shared_ptr vs. auto_ptr


In message <40e6a532.127360635@news.west.earthlink.net>, David Harmon
<source@netcom.com.invalid> writes[color=blue]
>On Sat, 19 Jun 2004 20:23:26 GMT in comp.lang.c++, alfps@start.no (Alf
>P. Steinbach) wrote,[color=green]
>>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.[/color]
>
>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.
>[/color]
For the _really_ simple things, consider boost::scoped_ptr. No
overheads, no sharing or transfer of ownership at all.

--
Richard Herring
Closed Thread


Similar C / C++ bytes