Std::auto_ptr to std::unique_ptr?

You cannot do a global find/replace because you can copy an auto_ptr (with known consequences), but a unique_ptr can only be moved. Anything that looks like.

You cannot do a global find/replace because you can copy an auto_ptr (with known consequences), but a unique_ptr can only be moved. Anything that looks like std::auto_ptr p(new int); std::auto_ptr p2 = p; will have to become at least like this std::unique_ptr p(new int); std::unique_ptr p2 = std::move(p); As for other differences, unique_ptr can handle arrays correctly (it will call delete, while auto_ptr will attempt to call delete.

8 on the other hand, doing this find/replace will only result in compile errors, it won't silently break code as far as I can see. So it is safe to do, if you manually fix the compile errors afterwards – jalf Aug 10 '10 at 16:43 1 @jalf: Indeed, I can't think of a counter-example that would be well-defined with auto_ptrs and UB with unique_ptrs. – Cubbi Aug 10 '10 at 17:25.

Std::auto_ptr and std::unique_ptr are incompatible in someways an a drop in replacement in others. So, no find/replace isn't good enough. However, after a find/replace working through the compile errors should fix everything except wierd corner cases.

Function variable: 100% compatible, as long as you don't pass it to a function or use it as a return value. Return value: not 100% compatible but 99% compatible doesn't seem wrong. Parameter by value: 100% compatible with one caveat, unique_ptrs must be passed through a std::move call.

Parameter by reference: 100% compatible. Member variable: This one is tricky. Std::auto_ptrs copy semantics are evil.

If the class disallows copying then std::unique_ptr is a drop in replacement. However, if you tried to give the class reasonable copy semantics, you'll need to change the std::auto_ptr handling code. If you allowed copying of a class with a std::auto_ptr member without any special code, shame on you and good luck.In summary, std::unique_ptr is an unbroken std::auto_ptr.

It disallows at compile time behaviors that were often errors when using a std::auto_ptr. So if you used std::auto_ptr with care it needed, switching to std::unique_ptr should be simple. If you relied on std::auto_ptr's odd behavior, then you need to refactor your code anyway.

2 +1 for "you need to refactor your code anyway". Auto_ptrs are only good for what 20.4.5/3 says they are good for. – Cubbi Aug 10 '10 at 17:22 1 Let me add to this that you should, by all means, replace auto_ptr by unique_ptr in your code and fix the compilation errors.

You'd be surprised how many bugs this will uncover. – Bartosz Milewski Nov 30 '10 at 23:07.

AFAIK, unique_ptr is not a direct replacement. The major flaw that it fixes is the implicit transfer of ownership. Std::auto_ptr a(new int(10)), b; be = a; //implicitly transfers ownership std::unique_ptr a(new int(10)), b; be = std::move(a); //ownership must be transferred explicitly On the other hand, unique_ptr will have completely new capabilities: they can be stored in containers.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions