GNU C ++如何检查-std = c ++ 0x是否有效?

我的系统编译器(gcc42)与我想要的TR1function正常工作,但试图支持比系统更新的编译器版本,尝试访问TR1头文件#error要求-std = c ++ 0x选项,因为它是如何与图书馆或一些集线器像这样的接口。

/usr/local/lib/gcc45/include/c++/bits/c++0x_warning.h:31:2: error: #error This file requires compiler and library support for the upcoming ISO C++ standard, C++0x. This support is currently experimental, and must be enabled with the -std=c++0x or -std=gnu++0x compiler options. 

提供一个额外的开关是没有问题的,在这个系统(FreeBSD)下支持GCC 4.4和4.5,但显然它改变了图片!

使用我的系统编译器(g ++ 4.2默认方言):

 #include <tr1/foo> using std::tr1::foo; 

使用较新的(4.5)版本的编译器与-std = c ++ 0x:

 #include <foo> using std::foo; 

有反正使用预处理器,我可以告诉如果g ++运行与C ++ 0xfunction启用?

像这样的事情是我正在寻找的东西

 #ifdef __CXX0X_MODE__ #endif 

但是我还没有在手册中或在网上find任何东西。

以这样的速度,我开始认为生活会更容易,使用Boost作为依赖,而不用担心TR4之前到来的新的语言标准…嘿嘿。

如果使用-std=c++0x编译,则将定义__GXX_EXPERIMENTAL_CXX0X__

看来,用gcc 4.4.4,只有一个预定义的macros提示-std = c ++ 0x有效:

 #define __GXX_EXPERIMENTAL_CXX0X__ 1 

我没有访问gcc 4.5.0,但你可以自己检查一下:

 [16:13:41 0 ~] $ g++ -E -dM -std=c++0x -x c++ /dev/null >b [16:13:44 0 ~] $ g++ -E -dM -std=c++98 -x c++ /dev/null >a [16:13:50 0 ~] $ diff -uab --- a 2010-06-02 16:13:50.200787591 +0200 +++ b 2010-06-02 16:13:44.456912378 +0200 @@ -20,6 +20,7 @@ #define __linux 1 #define __DEC32_EPSILON__ 1E-6DF #define __unix 1 +#define __GXX_EXPERIMENTAL_CXX0X__ 1 #define __LDBL_MAX_EXP__ 16384 #define __linux__ 1 #define __SCHAR_MAX__ 127 

对于单线命令呢,

 g++ -E -dM -std=c++98 -x c++ /dev/null > std1 && g++ -E -dM -std=c++0x -x c++ /dev/null > std2 && diff -u std1 std2 | grep '[+|-]^*#define' && rm std1 std2 

给你类似于:

 +#define __GXX_EXPERIMENTAL_CXX0X__ 1 

那么从gcc-4.7开始,你就可以检查__cplusplus:

“现在,G ++将预定义macros__cplusplus设置为正确的值,C ++ 98/03为199711L,C ++ 11为201103L”

这应该是正确的,符合标准的方式来做到这一点。 不幸的是,它不适用于野外安装的大多数gcc。