GNU C++ 如何检查 -std=c++0x 何时生效?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2958398/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
GNU C++ how to check when -std=c++0x is in effect?
提问by TerryP
My system compiler (gcc42) works fine with the TR1 features that I want, but trying to support newer compiler versions other than the systems, trying to accessing TR1 headers an #error demanding the -std=c++0x option because of how it interfaces with library or some hub bub like that.
我的系统编译器 (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.
Having to supply an extra switch is no problem, to support GCC 4.4 and 4.5 under this system (FreeBSD), but obviously it changes the picture!
必须提供一个额外的开关是没有问题的,在这个系统(FreeBSD)下支持 GCC 4.4 和 4.5,但显然它改变了画面!
Using my system compiler (g++ 4.2 default dialect):
使用我的系统编译器(g++ 4.2 默认方言):
#include <tr1/foo>
using std::tr1::foo;
Using newer (4.5) versions of the compiler with -std=c++0x:
使用带有 -std=c++0x 的较新 (4.5) 版本的编译器:
#include <foo>
using std::foo;
Is there anyway using the pre processor, that I can tell if g++ is running with C++0x features enabled?
有没有使用预处理器,我可以判断 g++ 是否在启用 C++0x 功能的情况下运行?
Something like this is what I'm looking for:
我正在寻找这样的东西:
#ifdef __CXX0X_MODE__
#endif
but I have not found anything in the manual or off the web.
但我没有在手册或网上找到任何内容。
At this rate, I'm starting to think that life would just be easier, to use Boost as a dependency, and not worry about a new language standard arriving before TR4... hehe.
按照这个速度,我开始认为生活会更轻松,使用 Boost 作为依赖项,而不用担心在 TR4 之前到来的新语言标准......呵呵。
采纳答案by James McNellis
If you compile with -std=c++0x
, then __GXX_EXPERIMENTAL_CXX0X__
will be defined.
如果用 编译-std=c++0x
,__GXX_EXPERIMENTAL_CXX0X__
则将被定义。
回答by nos
There seems, with gcc 4.4.4, to be only one predefined macro hinting that -std=c++0x is in effect:
对于 gcc 4.4.4,似乎只有一个预定义的宏暗示 -std=c++0x 有效:
#define __GXX_EXPERIMENTAL_CXX0X__ 1
I don't have access to gcc 4.5.0 , but you can check that one yourself:
我无权访问 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
For one-line command do,
对于单行命令,
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
gives you something like:
给你类似的东西:
+#define __GXX_EXPERIMENTAL_CXX0X__ 1
回答by emsr
Well, from gcc-4.7onwards you'll be able to check __cplusplus:
好吧,从gcc-4.7开始,您将能够检查 __cplusplus:
"G++ now sets the predefined macro __cplusplus to the correct value, 199711L for C++98/03, and 201103L for C++11"
“G++ 现在将预定义的宏 __cplusplus 设置为正确的值,C++98/03 为 199711L,C++11 为 201103L”
This should be the correct, standard-compliant way to do it. Unfortunately, it doesn't work for most gcc installed in the wild.
这应该是正确的、符合标准的方法。不幸的是,它不适用于安装在野外的大多数 gcc。