[cfe-dev] Using unique_ptr in both C++03 and C++11 mode?

Jeffrey Walton noloader at gmail.com
Mon Jul 27 07:23:43 PDT 2015


My apologies for asking a user question on CFE Devs... I bring it here
because the CFE Devs told me to do this a long time ago (cf.,
http://clang-developers.42468.n3.nabble.com/Suggestion-for-C-migration-tool-tr1-removal-td4030729.html),
and Marshall Clow stated TR1 is no longer since C++11 (cf.,
http://marshall.calepin.co/c-and-xcode-46.html).

#if (__cplusplus >= 201103L) // C++11
# include <memory>
#elif defined(__APPLE__) // C++03 and Apple
# include <tr1/memory>
#else // C++03 and everyone else
# include <memory>
#endif

// Manage auto_ptr warnings and deprecation in C++11
#if (__cplusplus >= 201103L)
  template<typename T>
    using auto_ptr = std::unique_ptr<T>;
#else
# if defined(__APPLE__)
  using std::tr1::auto_ptr;
# else
  using std::auto_ptr;
# endif // Apple
#endif // C++11

The above results in a compile failure:

$ make
c++ -std=c++11 -DNDEBUG -g2 -O3 -fPIC -march=native -Wall -Wextra
-pipe -c 3way.cpp
In file included ...
./smartptr.h:27:20: error: unknown type name 'unique_ptr'
  using auto_ptr = std::unique_ptr<T>;
                   ^
./smartptr.h:27:30: error: expected ';' after alias declaration
  using auto_ptr = std::unique_ptr<T>;

How do we manage auto_ptr/unique_ptr on Apple platforms under Clang?
What is the secret sauce I am missing?



More information about the cfe-dev mailing list