This simple program (when compiled on Linux) will CORRECTLY give two different answers based on whether it's compiled with -std=c++0x
or not.
Problem: I cannot reproduce the same thing on OS X (Mountain Lion, 10.8 SDK).
What am I missing?
#include <iostream>
#include <sstream>
class Thing : public std::ostringstream
{
public:
Thing() : std::ostringstream() {}
virtual ~Thing() { std::cerr << str(); }
};
int main(int argc, const char * argv[]) {
Thing() << "Hello" << std::endl;
return 0;
}
To see what I mean, do the following (on Linux first, just to see how it should work):
> g++ main.cpp
> ./a.out
0x401471
> g++ -std=c++0x main.cpp
> ./a.out
Hello
The first will print a hex address, the second will print "Hello". This is correct behavior and is because the operator <<
resolves to two different things (there are no rvalue references in C++03 so there you go).
Now, try the same thing on OS X:
> xcrun c++ main.cpp
> ./a.out
0x10840dd88
(This correctly produces the hex output.)
> xcrun c++ -std=c++0x main.cpp
> ./a.out
0x10840dd88
(Oops... still the hex output... We are in C++11x mode, but perhaps the correct headers are not being used?)
NOTE: Version of the compiler is here:
> xcrun c++ --version
Apple clang version 4.1 (tags/Apple/clang-421.11.66) (based on LLVM 3.1svn)
Target: x86_64-apple-darwin12.2.0
Thread model: posix
NOTE: This is not a C++ issue per se, but rather an OS X build issue. For those who are interested, the reason that it produces different results with C++03 and C++11 is highlighted below in one of the answers.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…