是否有预处理器指令用于检测C ++ 11x支持? [英] Is there a preprocessor directive for detecting C++11x support?

查看:144
本文介绍了是否有预处理器指令用于检测C ++ 11x支持?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果有一些代码,我想尽可能使用C + + 11x扩展,但有一个后备,如果不支持。目前OSX版本的GCC和VisualC编译器几乎没有支持C ++ 11x,所以我使用:

If have some code where I would like to use C++11x extensions as much as possible, but have a fallback if this is not supported. Currently the OSX version of GCC and the VisualC compiler has little to no support for C++11x, so I use:

#if (defined(__APPLE__) || (defined(_WIN32)))
   ...fallback code without C++11x ...
#else
   ... code using C++11x ...
#endif

特别是因为MacPorts DOES中的gcc编译器支持c ++ 11x。

And this works, but is not really the right thing to do, especially since the gcc compiler in MacPorts DOES support c++11x.

有一个 #define C11X_SUPPORTED 类型宏?也许只有GCC有一个

Is there a #define C11X_SUPPORTED type macro? Perhaps something only GCC has?

推荐答案

__ cplusplus code> 199711L 在这些支持C ++ 11的C ++ 11编译器中, 201103L 。这是否在实践中有很多帮助是另一个问题:大多数编译器只有一半,所以不应该将它定义为 201103L ,即使他们支持您感兴趣的功能对于编译器来说,这是不可预料的:编译器定义为 199711L ,不支持 export 用于模板。但是没有通过功能测试的标准功能。

__cplusplus should be defined as 199711L in pre-C++11 compilers, 201103L in those suporting C++11. Whether this is much help in practice is another question: most compilers are only halfway there, so shouldn't be defining it as 201103L, even if they support the features you are interested in. And it's not unheard of for a compiler to lie: a compiler which defines it as 199711L and doesn't support export for templates, for example. But there's no standard feature by feature test.

最简单的解决方案是不要使用任何特定的新功能,除非您能确定所有编译器都支持它。你必须写和支持回退代码;为什么要维护两个版本。此规则的一个例外可能是影响性能的新功能:无论编译器是否支持移动语义。在这种情况下,我建议一个编译器依赖包含文件,你自己写的基于编译器文档和个人测试;只是因为编译器可能记录它支持一个特定的功能并不意味着它的支持是无bug的。只需为每个目标编译器创建一个目录,将该文件放在那里并指定适当的 -I / I makefile或项目文件。

The simplest solution is just not to use any particular new feature until you can be sure that all compilers support it. You have to write and support the fallback code anyway; why maintain two versions. The one exception to this rule might be new features which impact on performance: whether the compiler supports move semantics or not. In such cases, I'd suggest a compiler dependent include file, which you write yourself based on the compiler documentation and personal tests; just because the compiler may document that it supports a specific feature doesn't mean that its support is bug-free. Just create a directory per targeted compiler, put this file there and specify the appropriate -I or /I option in your makefile or project file.

您的测试应该是:

#ifdef HAS_MOVE_SEMANTICS
...
#endif

而不仅仅是编译器,版本或其他。

rather than just on the compiler, version or whatever.

这篇关于是否有预处理器指令用于检测C ++ 11x支持?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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