Gnu C ++宏__cplusplus标准符合? [英] Gnu C++ macro __cplusplus standard conform?

查看:145
本文介绍了Gnu C ++宏__cplusplus标准符合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Gnu C ++编译器似乎将 __ cplusplus 定义为 1

  #include< iostream> 
int main(){
std :: cout<< __cplusplus<< std :: endl;
}

打印 1 在标准c ++模式中使用gcc,在C ++ 0x模式中使用 gcc 4.3.4 和gcc 4.7.0。 / p>

C ++ 11 FDIS在<16>预定义宏名称[cpp.predefined] 中说明


在编译C ++翻译单元时,值201103L 定义的名称 __ cplusplus (脚注:本标准的未来版本将以更大的值替换此宏的值。不合格的comp $ b pilers应使用最多五个十进制数字的值。)


旧的std C ++ 03也有类似的规则。



GCC故意将此设置为 1 ,因为它是不合格



通过该列表我认为如果我有一个C + + 11启用编译器,我可以使用 __ cplusplus 以检查便携式。但是使用 g ++ 这似乎不工作。我知道 ... EXPERIMENTAL ... 宏,但很好奇为什么 g ++ 定义 __ cplusplus 这样。



我的原始问题是在不同的空指针变体之间切换。像这样:

  #if __cplusplus> 201100L 
#define MYNULL nullptr
#else
#define MYNULL NULL
#endif

是否有一种简单而合理的便携式方式来实现此类切换?

解决方案

这是固定大约一个月前(对于gcc 4.7.0)。错误报告提供了一个有趣的阅读: http://gcc.gnu.org/bugzilla/show_bug.cgi ?id = 1773


The Gnu C++ compiler seems to define __cplusplus to be 1

#include <iostream> 
int main() {
  std::cout << __cplusplus << std::endl;
}

This prints 1 with gcc in standard c++ mode, as well as in C++0x mode, with gcc 4.3.4, and gcc 4.7.0.

The C++11 FDIS says in "16.8 Predefined macro names [cpp.predefined]" that

The name __cplusplus is defined to the value 201103L when compiling a C++ translation unit. (Footnote: It is intended that future versions of this standard will replace the value of this macro with a greater value. Non-conforming com- pilers should use a value with at most five decimal digits.)

The old std C++03 had a similar rule.

Is the GCC deliberatly setting this to 1, because it is "non-conforming"?

By reading through that list I thought that I could use __cplusplus to check in a portable way if I have a C++11 enabled compiler. But with g++ this does not seem to work. I know about the ...EXPERIMENTAL... macro, but got curious why g++ is defining __cplusplus this way.

My original problem was switch between different null-pointer-variants. Something like this:

#if __cplusplus > 201100L
#  define MYNULL nullptr
#else
#  define MYNULL NULL
#endif

Is there a simple and reasonably portable way to implement such a switch?

解决方案

This was fixed about a month ago (for gcc 4.7.0). The bug report makes for an interesting read: http://gcc.gnu.org/bugzilla/show_bug.cgi?id=1773

这篇关于Gnu C ++宏__cplusplus标准符合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆