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

David Chisnall David.Chisnall at cl.cam.ac.uk
Mon Jul 27 09:10:49 PDT 2015


On 27 Jul 2015, at 16:25, Jeffrey Walton <noloader at gmail.com> wrote:
> 
> Like I said, Apple fiddles with things incessantly....

Yes, you keep saying this, and it keeps being wrong (or, at least, completely irrelevant to the problem at hand).  You want something like this:

#if __has_include(<tr1/memory>) && (__cplusplus < 201103L)
#	include <tr1/memory>
using std::auto_ptr;
#else
#		include <memory>
#endif
#if __cplusplus >= 201103L
template<typename T> using auto_ptr = std::unique_ptr<T>;
#else
using std::auto_ptr;
#endif

This works on FreeBSD, Linux, and Mac OS X and contains no Apple-specific code, because it checks for the thing that you actually want to check (sort of - you may want to prefer unique_ptr if you’re using a C++11 standard library.  If so, rearrange the tests to put the C++11 check first) and not some random 

If you also want this to work with g++, then you’ll need to add another case for that, as most g++ versions don’t support __has_include (I think the latest ones do).  You can stick this in a configure-time check if you need to.

And you can then deal with the fallout from having different ABIs for C++11 and C++03 if anyone links against your code.

David





More information about the cfe-dev mailing list