Why is auto_ptr being deprecated?
The direct replacement for auto_ptr
(or the closest thing to one anyway) is unique_ptr
. As far as the "problem" goes, it's pretty simple: auto_ptr
transfers ownership when it's assigned. unique_ptr
also transfers ownership, but thanks to codification of move semantics and the magic of rvalue references, it can do so considerably more naturally. It also "fits" with the rest of the standard library considerably better (though, in fairness, some of that is thanks to the rest of the library changing to accommodate move semantics instead of always requiring copying).
The change in name is also (IMO) a welcome one -- auto_ptr
doesn't really tell you much about what it attempts to automate, whereas unique_ptr
is a fairly reasonable (if terse) description of what's provided.
Is auto_ptr deprecated?
UPDATE: This answer was written in 2010 and as anticipated std::auto_ptr
has been deprecated. The advice is entirely valid.
In C++0x std::auto_ptr
will be deprecated in favor of std::unique_ptr
. The choice of smart pointer will depend on your use case and your requirements, with std::unique_ptr
with move semantics for single ownership that can be used inside containers (using move semantics) and std::shared_ptr
when ownership is shared.
You should try to use the smart pointer that best fits the situation, choosing the correct pointer type provides other programmers with insight into your design.
Error : ‘templateclass class std::auto_ptr’ is deprecated
If you read some books of Scott Meyers, he strongly recommends not to use auto_ptr
. Actually, new compilers may restrict it's usage because of lots of possible problems with it when using auto_ptr
in STL containers, etc.
Instead, you should use std::unique_ptr
if you don't want more that one copy of the object, and std::shared_ptr
if you need to copy the pointer.
What's c++11 standard's equivalent for the deprecated std::auto_ptr?
unique_ptr
is the C++11 replacement facility for auto_ptr
. However, it's not a drop-in replacement, since auto_ptr
had transfer-of-ownership-on-copy semantics and unique_ptr
forces you to transfer ownership explicitly. When you have something like this:
auto_ptr<Foo> x(new Foo());
// ...
auto_ptr<Foo> y = x;
// ...
bar( y ); // copies y into bar(), transferring ownership
. . . to use unique_ptr
, you would need to add move()
to the ownership-transfer sites:
unique_ptr<Foo> x(new Foo());
// ...
unique_ptr<Foo> y = move(x);
// ...
bar( move(y) );
EDIT:
Without knowing what specific error you're getting, it's hard to tell why your default constructor is failing to compile. However, your copy constructor should fail to compile with unique_ptr
unless move
is added.
no warning: ‘auto_ptr’ is deprecated only when auto_ptr used just once
This a known bug of GCC 33911, with status NEW
.
c++ auto_ptr destroyed when passed into a function
Is the pointer copied when passed into UsePointer function? Hence the owernship is transferred?
Yes. Unless the function parameter is reference qualified, arguments pass by value. For auto_ptr
that involves copying, and thus passing ownership.
What do I need to if want spObject not be destroyed? Do I need to pass this pointer by reference?
You could. But better yet, pass a reference to the object itself. A function shouldn't accept smart pointers unless manipulation of the ownership is involved. If it just needs to do something with pointee, accept a CSomeClass const&
and pass *spObject
.
Also this book is a bit outdated - does the same hold for unique_ptr in c++ 11?
Yes. With the difference that unique_ptr
is not copyable, and so cannot pass its ownership away implicitly. To pass a unique_ptr
, it must be moved explicitly. The appearance of std:move
in the code that passes the pointer into the function gives an explicit visual cue that ownership changes.
C++ std:.auto_ptr or std::unique_ptr (to support multiple compilers, even old C++03 compilers)?
As you noted, std::auto_ptr<> has been deprecated in C++11
(Reference).
Moving to c++11 std::unique_ptr<> is the right way, as also stated by Herb Sutter in
GotW89:
- What’s the deal with auto_ptr?
auto_ptr is most charitably characterized as a valiant attempt to create a unique_ptr before C++ had move semantics. auto_ptr is now deprecated, and should not be used in new code.
If you have auto_ptr in an existing code base, when you get a chance try doing a global search-and-replace of auto_ptr to unique_ptr; the vast majority of uses will work the same, and it might expose (as a compile-time error) or fix (silently) a bug or two you didn’t know you had.
Please also note that C++17 is going to remove std::auto_ptr.
I think there may be different ways of solving your problem, the "right" one also depends on how your actual code is written.
A few options are:
Option 1
Use boost::unique_ptr
Option 2
Conditionally use auto_ptr or unique_ptr based on __cplusplus.
class Myclass {
#if __cplusplus < 201103L
std::auto_ptr m_ptr;
#else
std::unique_ptr m_ptr;
#endif
...
This will be scattered in every place where you reference auto_ptr, I don't really like it.
May be look less awkward if all your references to std::auto_ptr are already typedef'ed (just conditionally change the typedef).
Option 3
Conditionally use using and aliasing to "define" auto_ptr (and reference it without std:: namespace).
#if __cplusplus < 201103L
using std::auto_ptr;
#else
template
using auto_ptr = std::unique_ptr;
#endif
Drawback: you keep using "auto_ptr", but in c++11 it means std::unique_ptr.
Really confusing...
Option 3.1
Probably slightly better than option 2:
reverse using aliases and prefer unique_ptr name.
Option 4
Wrap the std:: smart pointer (conditionally auto_ptr or unique_ptr) in your own defined template smart pointer class.
This may be cumbersome and requires search and replacement of all auto_ptr references with your new class.
Other dirty options
Other options involve definitions inside the std:: namespace, which I think is prohibited by the standard,
or using preprocessor #define to ...ehm... "rename" unique_ptr to auto_ptr just for old C++03 compilers.
Related Topics
Why Do Reference Type Members Cause Implicitly-Declared Copy Assignment Operator to Be Deleted
Why Do Objects Returned from Bind Ignore Extra Arguments
How to Implement Coroutines in C++
Which One Will Execute Faster, If (Flag==0) or If (0==Flag)
Is There Any Reason to Use C Instead of C++ for Embedded Development
How to Pass Arguments and Redirect Stdin from a File to Program Run in Gdb
Overload Operators as Member Function or Non-Member (Friend) Function
Better Shading on Bw Display While Rendering Filled Surfaces
C++ Read/Write Class From/To Binary File
What Is Linux's Native Gui API
In Cmake, How to Test If the Compiler Is Clang
C++ Boost: What's the Cause of This Warning
Is There a Functional Difference Between "2.00" and "2.00F"
Convert a Unicode String in C++ to Upper Case
Stopping the Debugger When a Nan Floating Point Number Is Produced