tags:

views:

189

answers:

3

I've recently started appreciating std::auto_ptr and now I read that it will be deprecated. I started using it for two situations:

  • Return value of a factory
  • Communicating ownership transfer

Examples:

// Exception safe and makes it clear that the caller has ownership.
std::auto_ptr<Component> ComponentFactory::Create() { ... }

// The receiving method/function takes ownership of the pointer. Zero ambiguity.
void setValue(std::auto_ptr<Value> inValue);

Despite the problematic copy semantics I find auto_ptr useful. And there doesn't seem to be an alternative for the above examples.

Should I just keep using it and later switch to std::unique_ptr? Or is it to be avoided?

A: 

I'd suggest you go use boost smart pointers.

John Smith
They don't solve the same problem. Boost is not the answer to everything, no matter how useful it is.
Omnifarious
boost smart pointers bear certain overhead with them. shared_ptr does an extra memory allocation, intrusive_ptr requires an embedded counter. there is no good substitute for auto_ptr yet.
Maxim Yegorushkin
What Omni said. None of boost's smart pointers support transfer of ownership, because the standard library already provides that functionality as well as is possible in the current standard. If `std::auto_ptr` works, using either of boost's shared pointers is almost certainly a mistake [`boost::scoped_ptr` might suffice, but those situations are relatively limited]
Dennis Zickefoose
I'm already using the boost smart pointers. For local and member variables I use scoped_ptr, for use in containers shared_ptr. However, these do not "communicate" ownership transfer.
StackedCrooked
@Maxim Yegorushkin - sometimes the extra allocation can be bypassed using boost::make_shared.
Kylotan
+4  A: 

It is so very very useful, despite it's flaws, that I'd highly recommend just continuing to use it and switching to unique_ptr when it becomes available.

::std::unique_ptr requires a compiler that supports rvalue references which are part of the C++0x draft standard, and it will take a little while for there to be really wide support for it. And until rvalue references are available, ::std::auto_ptr is the best you can do.

Having both ::std::auto_ptr and ::std::unique_ptr in your code might confuse some people. But you should be able to search and replace for ::std::unique_ptr when you decide to change it. You may get compiler errors if you do that, but they should be easily fixable. The top rated answer to this question about replacing ::std::auto_ptr with ::std::unique_tr has more details.

Omnifarious
But stay consistent within one project. There is nothing worse than having two types that do the same thing but with different names.
Martin York
Technically, `unique_ptr` is not a drop-in replacement for `auto_ptr`. Among other things, it changes a possible runtime crash into a definite compiler error. This is generally a good thing, so you'll probably want to do a find-replace once it is available, fixing any compiler errors this triggers. See http://stackoverflow.com/questions/3451099
Brian
@Brian - Thank you for that link. I will include your clarification and that link in my answer in the morning. :-)
Omnifarious
+1  A: 

deprecated doesn't mean it's going away, just that there will be a better alternative.

I'd suggest keeping using it on current code, but using the new alternative on new code (New programs or modules, not small changes to current code). Consistency is important

John Burton
It does mean that it *might* go away at some point in the future though.
jalf