We are testing a library under C++11 (i.e., -std=c++11). The library uses auto_ptr and this pattern:Foo* GetFoo(){    autoptr<Foo> ptr(new Foo);    // Initialize Foo    ptr->Initialize(...);    // Now configure remaining attributes    ptr->SomeSetting(...);    return ptr.release();}C++11 deprecated auto_ptr, so we want to move away from it.However, the code supports both C++03 and C++11, so its not a simple as yanking auto_ptr. It's also worth mentioning the library has no external dependencies. It uses C++03; and does not use Autotools, Cmake, Boost, ...How should we handle the design changes to move away from auto_ptr for C++11 while retaining compatibility with C++03?
How to handle design changes for auto_ptr deprecation in C++11?
design;c++;c++11
In most respects the std::unique_ptr was made to be drop in (but safer) replacement for std::auto_ptr, so there should be very few (if any) code changes required other than (as you ask) directing the code to use either unique_ptr or auto_ptr.There a few ways to do this (and each comes with its own list tradeoffs) below. Given the code sample provided, I would favour either of the first two options.Option 1#if __cplusplus >= 201103Ltemplate <typename T>using auto_ptr = std::unique_ptr<T>;#elseusing std::auto_ptr;#endifTradeoffs;You introduce the auto_ptr name into the global namespace; you can mitigate this by defining it is your own private namespaceOnce migrate to C++17 (I believe auto_ptr will be completely removed) you can more easily search and replaceOption 2template <typename T>struct my_ptr {    #if __cplusplus >= 201103L    typedef std::unique_ptr<T> ptr;    #else    typedef std::auto_ptr<T> ptr;    #endif};Tradeoffs;Probably more cumbersome to work with, all the current auto_ptr need to changed in the code to something like my_ptr<T>::ptrBetter safety the names are not being introduced into the global namespaceOption 3Somewhat controversial, but if you are prepared to put up with the caveats of having a std class as a base#if __cplusplus >= 201103Ltemplate <typename T>using my_ptr = std::unique_ptr<T>;#elsetemplate <typename T>class my_ptr : public std::auto_ptr<T> {  // implement the constructors for easier use  // in particular  explicit my_ptr( X* p = 0 ) : std::auto_ptr(p) {}};#endifTradeoffs;Don't try to use the inherited class where a virtual base (in particular w.r.t. the non-virtual destructor) would be expected. Not that this should be an issue in the case - but be aware of itAgain, code changesPotential namespace mismatches - it all depends on how the pointer class is used to begin withOption 4Wrap the pointers in a new class and aggregate the required functions to the membertemplate <typename T>class my_ptr { // could even use auto_ptr name?  #if __cplusplus >= 201103L  std::unique_ptr<T> ptr_;  #else  std::auto_ptr<T> ptr_;  #endif  // implement functions required...  T* release() { return ptr_.release(); }};Tradeoffs;A little extreme when all you really want is to swap the implementations out