为什么这个用于在c++ 11和TR1之间切换的预处理器宏不能工作?

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

本文关键字:预处理 处理器 工作 不能 之间 用于 c++ TR1 为什么      更新时间:2023-10-16

例如,我正在尝试:

#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

它在c++ tr1 CentOS 5.6上工作正常,在c++ 11支持的Xcode (mavericks)中工作正常,但在c++ 11 mac OS (mavericks)的命令行上似乎失败了。

edit要清楚,这在c++ 11编译器上是失败的。或者应该是c++ 11编译器。也许这就是问题所在。

错误是:

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

显然,它没有正确地评估#if __cplusplus>= 201103L。

编译器参数:

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

编译器版本:

$ 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

怎么了?也许有更好的办法?

您实际上不是在用c++ 11编译。您需要将-std=c++11传递给g++