3

I've read a reasonable amount in decent textbooks about the auto_ptr class. While I understand what it is, and how it gets you around the problem of getting exceptions in places like constructors, I am having trouble figuring out when someone would actually use it.

An auto_ptr can only hold a single type (no array new[] initialization is supported). It changes ownership when you pass it into functions or try and duplicate it (it's not a reference counting smart pointer).

What is a realistic usage scenario for this class give its limitations? It seems like most of the textbook examples of its use are reaching because there isn't even a reason to be using a pointer over a stack variable in most of the cases...

Anyway, I'll stop my rant - but if you can provide a short example/description or a link to a good usage scenario for this I'd be grateful. I just want to know where I should use it in practice in case I come across the situation - I like to practice what I learn so I remember it.

John Humphreys
  • 37,047
  • 37
  • 155
  • 255
  • If you have a class which manages its own, large, dynamic member, then rather than `delete`ing the member in the destructor, you better wrap it in an auto_ptr. That also makes copying by const-reference impossible, which is a good thing. If the type is dynamic, a factory may return the member by `auto_ptr`. It's not entirely useless! – Kerrek SB Aug 18 '11 at 15:11
  • Your books probably use trivial objects wrapped in an `auto_ptr` to demonstrate usage. In real code, objects may not be so trivial, I wouldn't discount immediately... – Nim Aug 18 '11 at 15:31

2 Answers2

7

I'll give you a short example for a good usage. Consider this:

auto_ptr<SomeResource> some_function() {
    auto_ptr<SomeResource> my_ptr = get_the_resource();

    function_that_throws_an_exception();

    return my_ptr;
}

The function that raises an exception would normally cause your pointer to be lost, and the object pointed to would not be deleted. With the auto_ptr this can't happen, since it is destroyed when it leaves the frame it was created, if it hasn't been assigned (for example with return).

Constantinius
  • 34,183
  • 8
  • 77
  • 85
  • 1
    Basically you are taking about transfer of ownership. Any situation where you would like to transfer the ownership of an object std::auto_ptr is great as it not only makes the transfer exception safe but the use of auto_ptr is a way of documenting the transfer. – Martin York Aug 18 '11 at 17:44
4

auto_ptr has been deprecated in the now finalized C++11 standard. Some of the replacements are already available through TR1 or the Boost libraries. Examples are shared_ptr and unique_ptr (scoped_ptr in boost).

pmr
  • 58,701
  • 10
  • 113
  • 156
  • Thanks for the info, it's nice to know that they're updating the smart pointers in the new standard :) – John Humphreys Aug 18 '11 at 15:32
  • `scoped_ptr` and `unique_ptr` are slightly different though. `scoped_ptr` can't be returned from function, nor do I think it can be moved (in C++11 parlance). `unique_ptr` requires the move semantics of C++11. But if you just need to hold a reference in a function, they're interchangeable. – AFoglia May 29 '12 at 22:35