boost::shared_ptr vs. auto_ptr

Discussion in 'C++' started by ctick, Jun 19, 2004.

  1. ctick

    ctick Guest

    Are there any advantages of using boost::shared_ptr other than auto_ptr from
    standard library?
     
    ctick, Jun 19, 2004
    #1
    1. Advertising

  2. * 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.

    --
    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?
     
    Alf P. Steinbach, Jun 19, 2004
    #2
    1. Advertising

  3. ctick

    David Harmon Guest

    On Sat, 19 Jun 2004 20:23:26 GMT in comp.lang.c++, (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.
     
    David Harmon, Jun 19, 2004
    #3
  4. ctick

    Siemel Naran Guest

    "ctick" <> wrote in message
    news:g61Bc.6352$OB3.4344@bgtnsc05-

    > Are there any advantages of using boost::shared_ptr other than auto_ptr

    from
    > standard library?


    You're comparing apples to oranges.
     
    Siemel Naran, Jun 20, 2004
    #4
  5. In message <>, David Harmon
    <> writes
    >On Sat, 19 Jun 2004 20:23:26 GMT in comp.lang.c++, (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.
    >

    For the _really_ simple things, consider boost::scoped_ptr. No
    overheads, no sharing or transfer of ownership at all.

    --
    Richard Herring
     
    Richard Herring, Jun 22, 2004
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. SerGioGio

    shared_ptr vs std::auto_ptr

    SerGioGio, Jul 3, 2003, in forum: C++
    Replies:
    3
    Views:
    5,492
    Alexander Terekhov
    Jul 3, 2003
  2. Markus Dehmann

    auto_ptr vs shared_ptr

    Markus Dehmann, Jun 1, 2005, in forum: C++
    Replies:
    3
    Views:
    10,590
    msalters
    Jun 2, 2005
  3. Toby Bradshaw
    Replies:
    6
    Views:
    1,820
    Kai-Uwe Bux
    Jun 2, 2006
  4. Colin Caughie
    Replies:
    1
    Views:
    766
    Shooting
    Aug 29, 2006
  5. Jarek Blakarz

    passing auto_ptr as shared_ptr

    Jarek Blakarz, Dec 22, 2012, in forum: C++
    Replies:
    2
    Views:
    507
    Jarek Blakarz
    Dec 22, 2012
Loading...

Share This Page