GNU C++如何检查-std = c ++ 0x何时生效?

Ter*_*ryP 37 c++ gcc g++ c++11

我的系统编译器(gcc42)可以正常使用我想要的TR1功能,但是试图支持除系统之外的新编译器版本,尝试访问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.
Run Code Online (Sandbox Code Playgroud)

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

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

#include <tr1/foo>
using std::tr1::foo;
Run Code Online (Sandbox Code Playgroud)

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

#include <foo>
using std::foo;
Run Code Online (Sandbox Code Playgroud)

无论如何使用预处理器,我可以判断g ++是否在启用C++ 0x功能的情况下运行?

我正在寻找的东西是这样的:

#ifdef __CXX0X_MODE__
#endif
Run Code Online (Sandbox Code Playgroud)

但是我没有在手册中或网上找到任何内容.

按照这个速度,我开始认为生活会更容易,使用Boost作为依赖,而不用担心在TR4之前到达的新语言标准......呵呵.

nos*_*nos 82

在gcc 4.4.4中,似乎只有一个预定义的宏暗示-std = c ++ 0x生效:

#define __GXX_EXPERIMENTAL_CXX0X__ 1
Run Code Online (Sandbox Code Playgroud)

我无法访问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 -u a b
--- 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
Run Code Online (Sandbox Code Playgroud)

对于单行命令,

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
Run Code Online (Sandbox Code Playgroud)

给你这样的东西:

+#define __GXX_EXPERIMENTAL_CXX0X__ 1
Run Code Online (Sandbox Code Playgroud)

  • +1:"给一个人一条鱼; 你今天喂他了.教一个人钓鱼; 而且你不必听他不停地抱怨他有多饿.":) (28认同)

Jam*_*lis 36

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

  • 直到GCC添加了新的__cplusplus定义的最便携的选项是添加'#if defined(__ GXX_EXPERIMENTAL_CXX0X__)|| __cplusplus> = 201103L' (14认同)

ems*_*msr 17

那么,从gcc-4.7开始,你将能够检查__cplusplus:

"G ++现在将预定义的宏__cplusplus设置为正确的值,199711L用于C++ 98/03,而201103L用于C++ 11"

这应该是正确的,符合标准的方法.不幸的是,它对大多数在野外安装的gcc都不起作用.

  • FWIW来自C++ 11 16.8/1. (2认同)