C++ 如何确定编译器使用的C++标准的版本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2324658/
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
How to determine the version of the C++ standard used by the compiler?
提问by jasonline
How do you determine what version of the C++ standard is implemented by your compiler? As far as I know, below are the standards I've known:
您如何确定您的编译器实现了哪个版本的 C++ 标准?据我所知,以下是我所知道的标准:
- C++03
- C++98
- C++03
- C++98
采纳答案by RED SOFT ADAIR
By my knowledge there is no overall way to do this. If you look at the headers of cross platform/multiple compiler supporting libraries you'll always find a lotof defines that use compiler specific constructs to determine such things:
据我所知,没有整体方法可以做到这一点。如果您查看跨平台/多编译器支持库的头文件,您总会发现许多使用特定于编译器的构造来确定此类内容的定义:
/*Define Microsoft Visual C++ .NET (32-bit) compiler */
#if (defined(_M_IX86) && defined(_MSC_VER) && (_MSC_VER >= 1300)
...
#endif
/*Define Borland 5.0 C++ (16-bit) compiler */
#if defined(__BORLANDC__) && !defined(__WIN32__)
...
#endif
You probably will have to do such defines yourself for all compilers you use.
您可能必须为您使用的所有编译器自己做这样的定义。
回答by pmr
From the Bjarne Stroustrup C++0x FAQ:
来自 Bjarne Stroustrup C++0x 常见问题解答:
__cplusplus
In C++0x the macro
__cplusplus
will be set to a value that differs from (is greater than) the current199711L
.
__cplusplus
在 C++0x 中,宏
__cplusplus
将被设置为一个不同于(大于)当前199711L
.
Although this isn't as helpful as one would like. gcc
(apparently for nearly 10 years) had this value set to 1
, ruling out one major compiler, until it was fixed when gcc 4.7.0 came out.
尽管这并不像人们希望的那样有帮助。gcc
(显然将近 10 年了)将此值设置为1
,排除了一个主要编译器,直到gcc 4.7.0 出来时它被修复。
These are the C++ standards and what value you should be able to expect in __cplusplus
:
这些是 C++ 标准以及您应该能够期待的价值__cplusplus
:
- C++ pre-C++98:
__cplusplus
is1
. - C++98:
__cplusplus
is199711L
. - C++98 + TR1: This reads as C++98 and there is no way to check that I know of.
- C++11:
__cplusplus
is201103L
. - C++14:
__cplusplus
is201402L
. - C++17:
__cplusplus
is201703L
.
- C++ C++98 之前的:
__cplusplus
是1
. - C++98:
__cplusplus
是199711L
。 - C++98 + TR1:这读作 C++98 并且没有办法检查我知道的。
- C++11:
__cplusplus
是201103L
。 - C++14:
__cplusplus
是201402L
。 - C++17:
__cplusplus
是201703L
。
If the compiler might be an older gcc
, we need to resort to compiler specific hackery (look at a version macro, compare it to a table with implemented features) or use Boost.Config(which provides relevant macros). The advantage of this is that we actually can pick specific features of the new standard, and write a workaround if the feature is missing. This is often preferred over a wholesale solution, as some compilers will claim to implement C++11, but only offer a subset of the features.
如果编译器可能是旧的gcc
,我们需要求助于编译器特定的技巧(查看版本宏,将其与具有已实现功能的表进行比较)或使用Boost.Config(提供相关宏)。这样做的好处是我们实际上可以选择新标准的特定功能,并在缺少该功能时编写解决方法。这通常比批发解决方案更受欢迎,因为一些编译器声称实现了 C++11,但只提供了一部分功能。
The Stdcxx Wiki hosts a comprehensive matrix for compiler support of C++0x features(if you dare to check for the features yourself).
Stdcxx Wiki为 C++0x 特性的编译器支持提供了一个综合矩阵(如果你敢自己检查这些特性)。
Unfortunately, more finely-grained checking for features (e.g. individual library functions like std::copy_if
) can only be done in the build system of your application (run code with the feature, check if it compiled and produced correct results - autoconf
is the tool of choice if taking this route).
不幸的是,更多的细晶粒检查功能(如单个库函数一样std::copy_if
)只能在你的应用程序(与功能运行代码,检查的编译系统来完成,如果它编译,产生正确的结果-autoconf
是,如果服用的首选工具这条路线)。
回答by Deepanshu
Please, run the following code to check the version.
请运行以下代码以检查版本。
#include<iostream>
int main() {
if (__cplusplus == 201703L) std::cout << "C++17\n";
else if (__cplusplus == 201402L) std::cout << "C++14\n";
else if (__cplusplus == 201103L) std::cout << "C++11\n";
else if (__cplusplus == 199711L) std::cout << "C++98\n";
else std::cout << "pre-standard C++\n";
}
回答by Bj?rn Pollex
Depending on what you want to achieve, Boost.Configmight help you. It does not provide detection of the standard-version, but it provides macros that let you check for support of specific language/compiler-features.
根据您想要实现的目标,Boost.Config可能会帮助您。它不提供对标准版本的检测,但它提供了让您检查对特定语言/编译器功能的支持的宏。
回答by Vinzenz
__cplusplus
In C++0x the macro __cplusplus will be set to a value that differs from (is greater than) the current 199711L.
__cplusplus
在 C++0x 中,宏 __cplusplus 将被设置为一个不同于(大于)当前 199711L 的值。
回答by dimon4eg
Use __cplusplus
as suggested.
Only one note for Microsoft compiler, use Zc:__cplusplus
compiler switch to enable __cplusplus
__cplusplus
按照建议使用。微软编译器只有一个注释,使用Zc:__cplusplus
编译器开关来启用__cplusplus
Source https://devblogs.microsoft.com/cppblog/msvc-now-correctly-reports-__cplusplus/
来源https://devblogs.microsoft.com/cppblog/msvc-now-correctly-reports-__cplusplus/
回答by TarmoPikaro
Normally you should use __cplusplus
define to detect c++17, but by default microsoft compiler does not define that macro properly, see https://devblogs.microsoft.com/cppblog/msvc-now-correctly-reports-__cplusplus/- you need to either modify project settings to include /Zc:__cplusplus
switch, or you could use syntax like this:
通常你应该使用__cplusplus
定义来检测 c++17,但默认情况下微软编译器没有正确定义该宏,请参阅https://devblogs.microsoft.com/cppblog/msvc-now-correctly-reports-__cplusplus/- 你需要修改项目设置以包含/Zc:__cplusplus
开关,或者您可以使用如下语法:
#if ((defined(_MSVC_LANG) && _MSVC_LANG >= 201703L) || __cplusplus >= 201703L)
//C++17 specific stuff here
#endif