compile errors with list of auto_ptr

Discussion in 'C++' started by gg, Aug 10, 2005.

  1. gg

    gg Guest

    I am getting the following compilation errors with the following
    program. My compiler is aCC 03.27 on HP-UX11 -

    #include <iostream>
    using namespace std;

    #include <list>
    #include <memory>
    #include <string>

    int main ( void )
    {
    auto_ptr < list < auto_ptr < string > > > list1;
    auto_ptr < list < auto_ptr < string > > > list2;

    auto_ptr < string > strAPT1 (new string ( "a" ) );
    auto_ptr < string > strAPT2 (new string ( "b" ) );
    auto_ptr < string > strAPT3 (new string ( "c" ) );

    list1->push_back ( strAPT1 );
    list1->push_back ( strAPT2 );
    list1->push_back ( strAPT3 );
    }


    Error 226: "/opt/aCC/include_std/memory", line 187 # No appropriate
    function found for call of 'auto_ptr::auto_ptr'. Last viable
    candidate was
    "std::auto_ptr<std::basic_string<char,std::char_traits<char>,std::allocator<char>
    >


    >::auto_ptr(std::basic_string<char,std::char_traits<char>,std::allocator<char> > *)" ["/opt/aCC/include_std/memory", line 830].

    Argument of type 'const
    std::auto_ptr<std::basic_string<char,std::char_traits<char>,std::allocator<char>
    > > &' could not be

    converted to
    'std::basic_string<char,std::char_traits<char>,std::allocator<char> >
    *'.
    new (__p) _TypeT (__val);
    ^^^^^^^^^^^^^^^^^^^^^^^^
    Error 556: "/opt/aCC/include_std/list", line 911 # Unable to generate
    specialization "void

    __rw::__rw_construct<std::auto_ptr<std::basic_string<char,std::char_traits<char>,std::allocator<char>
    >


    >,std::auto_ptr<std::basic_string<char,std::char_traits<char>,std::allocator<char> > >


    >(std::auto_ptr<std::basic_string<char,std::char_traits<char>,std::allocator<char> > > *,const


    std::auto_ptr<std::basic_string<char,std::char_traits<char>,std::allocator<char>
    > > &)" due to errors during generation.

    _RWSTD_VALUE_ALLOC (_C_value_alloc_type,
    ^^^^^^^^^^^^^^^^^^
    Error 556: "/opt/aCC/include_std/list", line 911 # Unable to generate
    specialization "void

    __rw::__rw_construct<std::auto_ptr<std::basic_string<char,std::char_traits<char>,std::allocator<char>
    >


    >,std::auto_ptr<std::basic_string<char,std::char_traits<char>,std::allocator<char> > >


    >(std::auto_ptr<std::basic_string<char,std::char_traits<char>,std::allocator<char> > > *,const


    std::auto_ptr<std::basic_string<char,std::char_traits<char>,std::allocator<char>
    > > &)" due to errors during generation.

    _RWSTD_VALUE_ALLOC (_C_value_alloc_type,
    ^^^^^^^^^^^^^^^^^^
    gg, Aug 10, 2005
    #1
    1. Advertising

  2. gg wrote:
    > I am getting the following compilation errors with the following
    > program. My compiler is aCC 03.27 on HP-UX11 -
    >
    > #include <iostream>
    > using namespace std;
    >
    > #include <list>
    > #include <memory>
    > #include <string>
    >
    > int main ( void )
    > {
    > auto_ptr < list < auto_ptr < string > > > list1;


    'std::auto_ptr' does not satisfy the requirements for objects to be
    contained in standard containers ('std::list' included).

    Get Boost and use 'boost::shared_ptr' for that.

    > [...]


    V
    Victor Bazarov, Aug 10, 2005
    #2
    1. Advertising

  3. gg

    Guest

    you are trying to store a pointer to std::list in an auto_ptr where as
    auto_ptr works only with pointers.

    thanks
    rt
    , Aug 11, 2005
    #3
  4. gg

    Guest


    > auto_ptr < list < auto_ptr < string > > > list1;
    > auto_ptr < list < auto_ptr < string > > > list2;
    >


    You should not try to store auto_ptr objects in STL containers.
    auto_ptr copy semantics are different from normal objects. Copying
    mutates the source auto_ptr and sets the underlying pointer to NULL.
    This algorithms on the container which depend on temporary copying of
    objects can fail on auto_ptr containers. An auto_ptr copy constructor /
    assignment operator prototype too is slightly different:

    auto_ptr(auto_ptr&); // see no X(const X&) but X(X&)

    If you have a list< auto_ptr<...> > and call insert on it with a new
    auto_ptr object, it won't compile. So the above code is incorrect and
    'auto_ptr's are not meant to be stored in STL containers.

    Cheers,
    Andy
    , Aug 11, 2005
    #4
  5. gg

    Guest

    > you are trying to store a pointer to std::list in an auto_ptr where as
    > auto_ptr works only with pointers.
    >


    I guess if you really ever allocate an std::list on the heap, you could
    use an auto_ptr to manage its heap-life. There is not problem there.
    , Aug 11, 2005
    #5
  6. gg

    msalters Guest

    schreef:

    > > auto_ptr < list < auto_ptr < string > > > list1;
    > > auto_ptr < list < auto_ptr < string > > > list2;
    > >

    >
    > You should not try to store auto_ptr objects in STL containers.
    > auto_ptr copy semantics are different from normal objects. Copying
    > mutates the source auto_ptr and sets the underlying pointer to NULL.
    > This algorithms on the container which depend on temporary copying of
    > objects can fail on auto_ptr containers. An auto_ptr copy constructor /
    > assignment operator prototype too is slightly different:
    >
    > auto_ptr(auto_ptr&); // see no X(const X&) but X(X&)
    >
    > If you have a list< auto_ptr<...> > and call insert on it with a new
    > auto_ptr object, it won't compile. So the above code is incorrect and
    > 'auto_ptr's are not meant to be stored in STL containers.


    Your explanation why it won't work is perfect, but let me point out
    that it's not just insert() that prevents it. Every function in list
    can break, including the default constructor! Implementations differ
    slightly, so you cannot say precisely which functions will break.

    Regards,
    Michiel Salters
    msalters, Aug 11, 2005
    #6
    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,537
    Dave Rahardja
    Jan 11, 2005
  2. George2

    compile error about auto_ptr

    George2, Feb 23, 2008, in forum: C Programming
    Replies:
    0
    Views:
    290
    George2
    Feb 23, 2008
  3. George2

    auto_ptr compile error

    George2, Mar 21, 2008, in forum: C Programming
    Replies:
    0
    Views:
    308
    George2
    Mar 21, 2008
  4. George2

    auto_ptr assignment compile error

    George2, Mar 30, 2008, in forum: C Programming
    Replies:
    0
    Views:
    431
    George2
    Mar 30, 2008
  5. Sousuke
    Replies:
    9
    Views:
    1,133
    Bart van Ingen Schenau
    Mar 16, 2010
Loading...

Share This Page