And the CFE Devs specifically told me to use that code;
No they didn't. They told you to do something similar if you want to use shared_ptr
, because for C++03 <tr1/memory>
defines std::tr1::shared_ptr
and for C++11 <memory>
defines std::shared_ptr
.
But you're not using shared_ptr
. If you want to use auto_ptr
then it's just std::auto_ptr
, everywhere, which is always defined in <memory>
.
I think you've misunderstood Marshall's comment and you're overcomplicating things. What you quoted ('In c++11, they are officially part of the standard, and live in the namespace std, just like vector and string. The include files no longer live in the "tr1" folder, either.') is not Apple-specific or Clang-specific, it applies to all compilers. But since auto_ptr
was never part of TR1 and never in <tr1/memory>
it's irrelevant that the contents of TR1 are now in namespace std
, because what you're trying to use was never included in TR1.
You should not be using TR1 at all here.
# include <memory>
// Manage auto_ptr warnings and deprecation in C++11
#if (__cplusplus >= 201103L)
template<typename T>
using auto_ptr = std::unique_ptr<T>;
#else
using std::auto_ptr;
#endif // C++11
This should be correct for modern compilers, but won't work on the stupid configuration that comes with XCode, which is a modern version of Clang that supports C++11 and the libstdc++ from GCC 4.2 which is nearly ten years old and doesn't support unique_ptr
.
To cope with the default OS X toolchain this works:
#include <memory>
#if __cplusplus >= 201103L
# ifdef __clang__
# if __has_include(<forward_list>)
// either using libc++ or a libstdc++ that's new enough to have unique_ptr
# define HAVE_UNIQUE_PTR 1
# endif
# else // not clang, assume unique_ptr available
# define HAVE_UNIQUE_PTR 1
# endif
#endif
#ifdef HAVE_UNIQUE_PTR
template<typename T> using auto_ptr = std::unique_ptr<T>;
#else
using std::auto_ptr;
#endif
This works by using the presence of <forward_list>
as an indicator of whether the standard library clang is using supports std::unique_ptr
.
If clang is using libc++ as its standard library then all versions support unique_ptr
and also provide <forward_list>
, so the test passes.
If clang is using libstdc++ then whether unique_ptr
is supported depends on the libstdc++ version. unique_ptr
was added to libstdc++ in GCC 4.3, which is the same version that added <forward_list>
, so if that header is available then unique_ptr
will be too. If you are using clang with the ancient libstdc++ that ships with the Apple toolchains (from GCC 4.2) then unique_ptr
is not supported, but neither is <forward_list>
, so the test fails and you use auto_ptr
instead.
That should work for any GCC/libstdc++, Clang/libc++ or Clang/libstdc++ combination found in the wild. I don't know what is needed for VC++/Dinkumware and Clang/Dinkumware, from your answer it looks like maybe you would just change the first condition to:
#if __cplusplus >= 201103L || _MSC_VER >= 1600