On OS X, simple C++ program gives incorrect results (which are a result of command-line options ‘c++03’ vs ‘c++11’)

Firstly, the expected difference in behaviour is because the operator<<(std::ostream&, const char*) overload (it’s actually a function template specialization, but nevermind for now) has a parameter of type std::ostream& and an lvalue reference can only bind to an lvalue, and in your example the stream is an rvalue so that overload can’t be used. In C++03 that means the only viable overload is the std::ostream::operator<<(const void*) member function, because member functions can be called on rvalue objects, so the string is written out as a void* address in hexadecimal. In C++11 there is a new operator<<(std::ostream&&, const T&) function template that allows writing to rvalue streams, and forwards to the operator<<(std::ostream&, const char*) overload, so the string is output rather than a hex address.

On GNU/Linux you are presumably using a fairly recent GCC release, which has fairly good support for C++11 in both the compiler (g++) and the standard library (libstdc++) so it has the operator<<(std::ostream&&, const T&) overload and everything Just Works.

On OS X you are probably using Clang with GCC’s standard library, libstdc++. Xcode ships with an ancient version of GCC by default (4.2) and the standard library from GCC 4.2 doesn’t support C++11, so doesn’t have the operator<< overload for rvalue streams. Using -std=c++0x tells Clang to support C++11 language features (such as rvalue references), but doesn’t magically make GCC 4.2’s library grow C++11 code that wasn’t even a twinkle in the standard committee’s eye when GCC 4.2 was released. Rather than shipping a non-prehistoric libstdc++ Apple instead wrote their own standard library implementation to go with LLVM and Clang projects. Using -stdlib=libc++ tells clang to use that libc++ standard library implementation instead of the ancient libstdc++. As libc++ was written recently it has the operator<< overload for rvalue references.

Leave a Comment