Search code examples
c++c++11c-preprocessortr1

Why does this preprocessor macro for toggling between C++11 and TR1 not work?


For instance, I'm trying this:

#if __cplusplus >= 201103L
#include <unordered_set>
typedef std::unordered_set<std::string> UnorderedStringSet;
#else
#include <tr1/unordered_set>
typedef std::tr1::unordered_set<std::string> UnorderedStringSet;
#endif

It works ok on c++ tr1 CentOS 5.6, and it works ok inside of C++11-aware Xcode (mavericks), but it seems to fail on the commandline on C+11 mac OS (mavericks).

edit To be clear, this is failing on a C++11 compiler. Or what should be a C++11 compiler. Maybe that's the problem.

The error is:

 MyFile.cpp  fatal error: 'tr1/unordered_set' file not found

Clearly it is not evaluating the #if __cplusplus >= 201103L correctly.

compiler args:

g++ -I. -I/snipped - -I/usr/include/php -I/usr/include/php/main -I/usr/include/php/TSRM -I/usr/include/php/Zend -I/usr/include/php/ext -I/usr/include/php/ext/date/lib -DHAVE_CONFIG_H -g -O2 -c /Users/xxxxx/src/MyFile.cpp  -fno-common -DPIC -o src/.libs/MyFile.o

compiler version:

$ g++ --version
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 5.1 (clang-503.0.40) (based on LLVM 3.4svn)
Target: x86_64-apple-darwin13.2.0
Thread model: posix

What could be wrong? And maybe is there a better way?


Solution

  • You're not actually compiling with C++11. You need to pass -std=c++11 to g++.