compile errors with list of auto_ptr

G

gg

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
::auto_ptr(std::basic_string<char,std::char_traits<char>,std::allocator<char> > *)" ["/opt/aCC/include_std/memory", line 830].
Argument of type 'const
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

,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
_RWSTD_VALUE_ALLOC (_C_value_alloc_type,
^^^^^^^^^^^^^^^^^^
Error 556: "/opt/aCC/include_std/list", line 911 # Unable to generate
specialization "void

,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
_RWSTD_VALUE_ALLOC (_C_value_alloc_type,
^^^^^^^^^^^^^^^^^^
 
V

Victor Bazarov

gg said:
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
 
R

ravinderthakur

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

thanks
rt
 
G

garth_rockett

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
 
G

garth_rockett

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.
 
M

msalters

(e-mail address removed) schreef:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top