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

Jeffrey Walton noloader at gmail.com
Mon Jul 27 11:07:09 PDT 2015


On Mon, Jul 27, 2015 at 12:10 PM, David Chisnall
<David.Chisnall at cl.cam.ac.uk> wrote:
> 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

My apologies.

Something is not right here, and I'm frustrated that I have wasted
hours on something that should have taken minutes.

> ... 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 fails under test case 3 for me. That's the one:

    c++ -std=c++11 -c test-clapple.cxx

Below are the results and the test program again.

**********

$ c++ -std=c++11 -c test-clapple.cxx
test-clapple.cxx:13:10: warning: C++11 [-W#pragma-messages]
# pragma message "C++11"
         ^
test-clapple.cxx:21:10: warning: no libc++ [-W#pragma-messages]
# pragma message "no libc++"
         ^
test-clapple.cxx:25:10: warning: Apple build [-W#pragma-messages]
# pragma message "Apple build"
         ^
test-clapple.cxx:57:44: error: no type named 'unique_ptr' in namespace 'std'
template<typename T> using auto_ptr = std::unique_ptr<T>;
                                      ~~~~~^
test-clapple.cxx:57:54: error: expected ';' after alias declaration
template<typename T> using auto_ptr = std::unique_ptr<T>;
                                                     ^
                                                     ;

**********

$ c++ --version
Apple LLVM version 5.1 (clang-503.0.40) (based on LLVM 3.4svn)
Target: x86_64-apple-darwin12.6.0

**********

$ cat test-clapple.cxx
// Need to test {C++03,C++11} x {libc++, no libc++}

// c++ -c test-clapple.cxx
//     - OK
// c++ -stdlib=libc++ -c test-clapple.cxx
//     - OK
// c++ -std=c++11 -c test-clapple.cxx
//     - FAILS, no type named 'unique_ptr' in namespace 'std'
// c++ -std=c++11 -stdlib=libc++ -c test-clapple.cxx
//     - OK

#if (__cplusplus >= 201103L)
# pragma message "C++11"
#elif (__cplusplus >= 199711L)
# pragma message "C++03"
#endif

#if (_LIBCPP_VERSION)
# pragma message "libc++"
#else
# pragma message "no libc++"
#endif

#if defined(__apple_build_version__)
# pragma message "Apple build"
#else
# pragma message "non-Apple build"
#endif

#if 0
// 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
using std::auto_ptr;
#endif // C++11
#endif // 0

#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

int main(int argc, char* argv[])
{
    return argc;
}



More information about the cfe-dev mailing list