Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > auto_ptr inside vector does not compile

Reply
Thread Tools

auto_ptr inside vector does not compile

 
 
Jarek Blakarz
Guest
Posts: n/a
 
      01-25-2013
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;
}
 
Reply With Quote
 
 
 
 
SG
Guest
Posts: n/a
 
      01-25-2013
On Jan 25, 3:53*pm, Jarek Blakarz <(E-Mail Removed)> 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!
 
Reply With Quote
 
 
 
 
Balog Pal
Guest
Posts: n/a
 
      01-25-2013
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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
explicit auto_ptr<T>::auto_ptr(T*) ? Sousuke C++ 9 03-16-2010 11:54 AM
cant compile on linux system.cant compile on cant compile onlinux system. Nagaraj C++ 1 03-01-2007 11:18 AM
Free memory allocate by a STL vector, vector of vector, map of vector Allerdyce.John@gmail.com C++ 8 02-18-2006 12:48 AM
compile errors with list of auto_ptr gg C++ 5 08-11-2005 04:21 PM
auto_ptr<Derived> to auto_ptr<Base> Siemel Naran C++ 2 01-11-2005 04:45 AM



Advertisments