auto_ptr inside vector does not compile

Discussion in 'C++' started by Jarek Blakarz, Jan 25, 2013.

  1. Hi

    I'm trying to compile the following program.
    It does not compile.
    Unfortunately I don't understand the compile error messages.
    Please help me understanding what's going wrong and give me some indications of
    how to fix it.

    thanks

    #include <memory>
    #include <vector>
    #include <iostream>
    #include <string>

    using namespace std;

    class X {};

    int main(void)
    {
    vector<auto_ptr<X> > vecXP;
    auto_ptr<X> xPtr(new X);
    vecXP . push_back(xPtr);

    auto_ptr<auto_ptr<X> > elem = vecXP[0]; // compiler error

    return 0;
    }
    Jarek Blakarz, Jan 25, 2013
    #1
    1. Advertising

  2. Jarek Blakarz

    SG Guest

    On Jan 25, 3:53 pm, Jarek Blakarz <> wrote:
    >
    > I'm trying to compile the following program.
    > It does not compile.
    > Unfortunately I don't understand the compile error messages.


    which one?

    > Please help me understanding what's going wrong and give
    > me some indications of how to fix it.


    >   vector<auto_ptr<X> > vecXP;
    >   auto_ptr<X> xPtr(new X);
    >   vecXP . push_back(xPtr);
    >   auto_ptr<auto_ptr<X> > elem = vecXP[0];   // compiler error


    auto_ptr has a destructive copy constructor and assignment operator
    which is something that the standard containers have trouble dealing
    with. Don't ever try to put an auto_ptr into a container. It just
    won't work. Also, stop using auto_ptr altogether. As of C++11, it's
    deprecated. Its replacement (unique_ptr) is much better behaved.

    Cheers!
    SG, Jan 25, 2013
    #2
    1. Advertising

  3. Jarek Blakarz

    Balog Pal Guest

    On 1/25/2013 3:53 PM, Jarek Blakarz wrote:
    > I'm trying to compile the following program.
    > It does not compile.
    > Unfortunately I don't understand the compile error messages.
    > Please help me understanding what's going wrong and give me some indications of
    > how to fix it.


    You shall read up on how to use the standard library. It's made up of
    templates, but it imposes requirements on types to use them with. You
    are responsible to obey the requirements, otherwise you face undefined
    behavior. That can manifest at runtime, at compile time or whatever.

    The types that you can use with standard collections must be assignable
    and copyconstructible. That in practice means a copy of the type will be
    equivalent to the original. For auto_ptr it is not true. So in short you
    MUST NOT use auto_ptr in a std collection including vector.

    Try some suitable smart pointer like shared_ptr (from C++11 or TR1 or if
    you have neither from boost). That works fine in a vector.
    Balog Pal, Jan 25, 2013
    #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. Siemel Naran

    auto_ptr<Derived> to auto_ptr<Base>

    Siemel Naran, Jan 10, 2005, in forum: C++
    Replies:
    2
    Views:
    1,552
    Dave Rahardja
    Jan 11, 2005
  2. gg
    Replies:
    5
    Views:
    455
    msalters
    Aug 11, 2005
  3. Replies:
    8
    Views:
    1,914
    Csaba
    Feb 18, 2006
  4. George2

    compile error about auto_ptr

    George2, Feb 23, 2008, in forum: C Programming
    Replies:
    0
    Views:
    297
    George2
    Feb 23, 2008
  5. Sousuke
    Replies:
    9
    Views:
    1,144
    Bart van Ingen Schenau
    Mar 16, 2010
Loading...

Share This Page