passing auto_ptr as shared_ptr

Discussion in 'C++' started by Jarek Blakarz, Dec 22, 2012.

  1. Hi

    I would like to pass std::auto_ptr as boost::shared_ptr.
    When I do that I receive the following compile error:
    conversion from ‘std::auto_ptr<Human>’ to non-scalar type ‘boost::shared_ptr<Human>’ requested

    How can I do that correctly ?

    #include <memory>
    #include <boost/shared_ptr.hpp>

    using namespace std;
    using namespace boost;

    class Human {};

    void fun (shared_ptr<Human> s) {}

    int main(void)
    {
    auto_ptr<Human> hAP (new Human);
    fun (hAP);

    return 0;
    }

    thanks for help
    Jarek Blakarz, Dec 22, 2012
    #1
    1. Advertising

  2. On 12/22/2012 7:07 AM, Jarek Blakarz wrote:
    > I would like to pass std::auto_ptr as boost::shared_ptr.


    You can't. Those are unrelated types.

    > When I do that I receive the following compile error:
    > conversion from ‘std::auto_ptr<Human>’ to non-scalar type ‘boost::shared_ptr<Human>’ requested
    >
    > How can I do that correctly ?


    What's your definition of "correctly"?

    > #include <memory>
    > #include <boost/shared_ptr.hpp>
    >
    > using namespace std;
    > using namespace boost;
    >
    > class Human {};
    >
    > void fun (shared_ptr<Human> s) {}
    >
    > int main(void)
    > {
    > auto_ptr<Human> hAP (new Human);
    > fun (hAP);
    >
    > return 0;
    > }


    'std::auto_ptr' template has a 'get' function that returns the actual
    pointer to the object. That member doesn't release the ownership, (like
    the 'release' member would). I don't know how 'shared_ptr' is
    constructed, but it probably take a naked pointer as its argument. So, try

    fun(hAP.get());

    V
    --
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Dec 22, 2012
    #2
    1. Advertising

  3. On Saturday, December 22, 2012 2:37:26 PM UTC+1, Paavo Helde wrote:
    > Jarek Blakarz <> wrote in
    >
    > news::
    >
    >
    >
    > > Hi

    >
    > >

    >
    > > I would like to pass std::auto_ptr as boost::shared_ptr.

    >
    > > When I do that I receive the following compile error:

    >
    > > conversion from ‘std::auto_ptr<Human>’ to non-scalar type

    >
    > > ‘boost::shared_ptr<Human>’ requested

    >
    > >

    >
    > > How can I do that correctly ?

    >
    >
    >
    > You can't. The lifetime of the object is managed either by the auto_ptr or
    >
    > shared_ptr mechanism. You have to choose which one you want to use, nothing
    >
    > good would happen if both mechanisms attempted to manage the object at the
    >
    > same time.
    >
    >
    >
    > However, you can easily transfer the ownership from an auto_ptr to a
    >
    > shared_ptr, if that's what you want to do:
    >
    >
    >
    > auto_ptr<Human> hAP (new Human);
    >
    >
    >
    > shared_ptr<Human> s(hAP.release());
    >
    >
    >
    > Note that hAP is now NULL and is not useful for anything any more.
    >
    >
    >
    > hth
    >
    > Paavo


    The transfer of ownership from auto_ptr to shared_ptr makes sense. The
    following 2 member functions have been defined inside a shared_ptr to
    accomplish that:
    explicit shared_ptr(std::auto_ptr<Y> & r): px(r.get()), pn()
    shared_ptr & operator=( std::auto_ptr<Y> & r )

    I have a feeling that the fun(hAP) also makes sense and that's why I feel some
    kind of inconsistency why the conversion operator has not been defined.

    Please correct me if I'm wrong.
    Jarek Blakarz, Dec 22, 2012
    #3
    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,435
    Alexander Terekhov
    Jul 3, 2003
  2. ctick
    Replies:
    4
    Views:
    8,952
    Richard Herring
    Jun 22, 2004
  3. Siemel Naran

    auto_ptr<Derived> to auto_ptr<Base>

    Siemel Naran, Jan 10, 2005, in forum: C++
    Replies:
    2
    Views:
    1,536
    Dave Rahardja
    Jan 11, 2005
  4. Markus Dehmann

    auto_ptr vs shared_ptr

    Markus Dehmann, Jun 1, 2005, in forum: C++
    Replies:
    3
    Views:
    10,399
    msalters
    Jun 2, 2005
  5. Colin Caughie
    Replies:
    1
    Views:
    696
    Shooting
    Aug 29, 2006
Loading...

Share This Page