Questions tagged [auto-ptr]

A C++ template class that provides a limited garbage collection facility for pointers, by allowing pointers to have the elements they point to automatically destroyed when the auto_ptr object is itself destroyed. Deprecated as of C++11 in favor of unique_ptr.

auto_ptr is deprecated as of C++11. For the C++11 equivalent, unique_ptr, see the reference page.

Reference page for auto_ptr.

194 questions
219
votes
6 answers

Why is it wrong to use std::auto_ptr<> with standard containers?

Why is it wrong to use std::auto_ptr<> with standard containers?
Uhall
  • 5,153
  • 7
  • 22
  • 19
195
votes
4 answers

std::auto_ptr to std::unique_ptr

With the new standard coming (and parts already available in some compilers), the new type std::unique_ptr is supposed to be a replacement for std::auto_ptr. Does their usage exactly overlap (so I can do a global find/replace on my code (not that…
Martin York
  • 234,851
  • 74
  • 306
  • 532
98
votes
5 answers

Why is auto_ptr being deprecated?

I heard auto_ptr is being deprecated in C++11. What is the reason for this? Also I would like to know the difference between auto_ptr and shared_ptr.
brett
  • 4,759
  • 11
  • 38
  • 44
84
votes
4 answers

Is auto_ptr deprecated?

Will auto_ptr be deprecated in incoming C++ standard? Should unique_ptr be used for ownership transfer instead of shared_ptr? If unique_ptr is not in the standard, then do I need to use shared_ptr instead?
dimba
  • 24,103
  • 28
  • 127
  • 188
45
votes
3 answers

So can unique_ptr be used safely in stl collections?

I am confused with unique_ptr and rvalue move philosophy. Let's say we have two collections: std::vector> autoCollection; std::vector> uniqueCollection; Now I would expect the following to fail, as there is…
DanDan
  • 10,037
  • 8
  • 49
  • 68
25
votes
1 answer

std::unique_ptr vs std::shared_ptr vs std::weak_ptr vs std::auto_ptr vs raw pointers

What are the equivalent uses of each smart pointer in comparison to similar (but not limited to) some advanced techniques using raw pointers? My understanding is minimal, but from what I can gather: Raw Pointers: Only use if you really, really,…
Casey
  • 8,686
  • 9
  • 52
  • 79
21
votes
4 answers

Why doesn't auto_ptr have operator!() defined?

The Title pretty much sums up my question. Why can't the following be done to check for a null pointer? auto_ptr p( some_expression ); // ... if ( !p ) // error This must be done instead: if ( !p.get() ) // OK Why doesn't auto_ptr
Paul J. Lucas
  • 6,511
  • 5
  • 41
  • 77
19
votes
1 answer

Deletion of pointer to incomplete type and smart pointers

When trying to use an auto_ptr with a type that was declared with forward-declaration, like this: class A; ... std::auto_ptr a; the destructor of A is not called (apparently, because auto_ptr internally deletes the underlying pointer and the…
user500944
19
votes
5 answers

Smart pointers in container like std::vector?

I am learning about smart pointers (std::auto_ptr) and just read here and here that smart pointers (std::auto_ptr) should not be put in containers (i.e. std::vector) because even most compilers won't complain and it might seem correct. There is no…
nacho4d
  • 39,335
  • 42
  • 151
  • 231
18
votes
3 answers

When would you use an std::auto_ptr instead of boost::shared_ptr?

We've pretty much moved over to using boost::shared_ptr in all of our code, however we still have some isolated cases where we use std::auto_ptr, including singleton classes: template < typename TYPE > class SharedSingleton { public: static…
Alan
  • 12,874
  • 9
  • 40
  • 50
16
votes
9 answers

std::auto_ptr or boost::shared_ptr for pImpl idiom?

When using the pImpl idiom is it preferable to use a boost:shared_ptr instead of a std::auto_ptr? I'm sure I once read that the boost version is more exception friendly? class Foo { public: Foo(); private: struct impl; …
Rob
  • 70,036
  • 53
  • 151
  • 189
15
votes
4 answers

Using auto_ptr<> with array

I'm using auto_ptr<> which uses an array of class pointer type so how do I assign a value to it. e.g. auto_ptr arr[10]; How can I assign a value to the arr array?
balu
  • 169
  • 1
  • 4
15
votes
4 answers

Is there any reason to use auto_ptr?

After reading Jossutis' explanation on auto_ptr from his STL book I've got a strong impression that whatever task I would try to use it in I'd 100% fail becuase of one of many auto_ptr's pitfalls. My question is: are there any real life tasks where…
lithuak
  • 5,409
  • 8
  • 38
  • 52
14
votes
8 answers

Is it wrong to use auto_ptr with new char[n]

If I declare a temporary auto deleted character buffer using std::auto_ptr buffer(new char[n]); then the buffer is automatically deleted when the buffer goes out of scope. I would assume that the buffer is deleted using delete. However the…
David Sykes
  • 43,314
  • 17
  • 65
  • 77
12
votes
7 answers

C++: auto_ptr + forward declaration?

I have a class like this: class Inner; class Cont { public: Cont(); virtual ~Cont(); private: Inner* m_inner; }; in the .cpp, the constructor creates an instance of Inner with new and the destructor deletes it. This is working pretty…
shoosh
  • 70,450
  • 50
  • 199
  • 310
1
2 3
12 13