如何检查 C++11 支持?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5047971/
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 do I check for C++11 support?
提问by Maxpm
Is there a way to detect at compile-time if the compiler supports certain features of C++11? For example, something like this:
有没有办法在编译时检测编译器是否支持 C++11 的某些特性?例如,这样的事情:
#ifndef VARIADIC_TEMPLATES_SUPPORTED
#error "Your compiler doesn't support variadic templates. :("
#else
template <typename... DatatypeList>
class Tuple
{
// ...
}
#endif
回答by Cygon
There is a constant named __cplusplus
that C++ compilers should set to the version of the C++ standard supported see this
有一个名为__cplusplus
C++ 编译器应设置为支持的 C++ 标准版本的常量,请参阅此
#if __cplusplus <= 199711L
#error This library needs at least a C++11 compliant compiler
#endif
It is set to 199711L in Visual Studio 2010 SP1, but I do not know if vendors will be so bold to increase it already if they just have (partial) compiler-level support versus a standard C++ library with all the C++11 changes.
它在 Visual Studio 2010 SP1 中设置为 199711L,但我不知道供应商是否会如此大胆地增加它,如果他们只是拥有(部分)编译器级别的支持,而不是具有所有 C++11 更改的标准 C++ 库.
So Boost's defines mentioned in another answer remain the only sane way to figure out if there is, for example, support for C++11 threads and other specific parts of the standard.
因此,另一个答案中提到的 Boost 定义仍然是确定是否支持 C++11 线程和标准的其他特定部分的唯一合理方法。
回答by Paolo M
As stated by the C++11standard (§iso.16.8):
正如C++11标准 (§iso.16.8) 所述:
The name __cplusplusis defined to the value 201103Lwhen compiling a C++ translation unit.
在编译 C++ 翻译单元时,名称__cplusplus被定义为值201103L。
With the value of that macro, you can check whether the compiler is C++11 compliant.
使用该宏的值,您可以检查编译器是否符合 C++11。
Now, if you are looking for a standard way to check if the compiler supports a whatsoever subset of C++11 features, I think there is no standard, portable way; you can check compilers documentation or std library header files to get more information.
现在,如果您正在寻找一种标准方法来检查编译器是否支持 C++11 特性的任何子集,我认为没有标准的、可移植的方法;您可以查看编译器文档或标准库头文件以获取更多信息。
回答by Jarryd
I know that this is a very old question, but this question might be often seen, and the answers are a bit out of date.
我知道这是一个很老的问题,但是这个问题可能经常看到,答案有点过时了。
Newer compilers with the C++14 standard have a standard way to check features, including C++11 features. A comprehensive page is at https://isocpp.org/std/standing-documents/sd-6-sg10-feature-test-recommendations
具有 C++14 标准的较新编译器具有检查特性的标准方法,包括 C++11 特性。综合页面位于https://isocpp.org/std/standing-documents/sd-6-sg10-feature-test-recommendations
In summary, each feature has a standard macro defined that you can check with #ifdef
. For example, to check for user defined literals, you can use
总之,每个功能都定义了一个标准宏,您可以使用#ifdef
. 例如,要检查用户定义的文字,您可以使用
#ifdef __cpp_user_defined_literals
回答by kurono267
For check support C++14 and other. Testing on GCC 5.2.1.
用于检查支持 C++14 和其他。在 GCC 5.2.1 上测试。
#include <iostream>
int main(){
#if __cplusplus==201402L
std::cout << "C++14" << std::endl;
#elif __cplusplus==201103L
std::cout << "C++11" << std::endl;
#else
std::cout << "C++" << std::endl;
#endif
return 0;
}
回答by Donald Duck
You can use this:
你可以使用这个:
#if __cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1900)
cout << "C++11 is supported";
#else
cout << "C++11 is not supported";
#endif
For C++11, most compilers except Visual Studio set the __cplusplus
macro at 201103L
, but any version of Visual Studio sets it at 199711L
which is the value used for other compilers for before C++11. This code compares the _cplusplus
macro with 201103L
for all compilers except Visual Studio, and if the compiler is Visual Studio, it checks if the version of Visual Studio is later than 2015, the first version of Visual Studio which completely supports C++11 (for Visual Studio 2015, the _MSC_VER
macro has the value 1900
, see this answer).
对于 C++11,除 Visual Studio 之外的大多数编译器将__cplusplus
宏设置为201103L
,但任何版本的 Visual Studio 将其设置199711L
为 C++11 之前的其他编译器使用的值。此代码将_cplusplus
宏与201103L
除 Visual Studio 之外的所有编译器进行比较,如果编译器是 Visual Studio,它会检查 Visual Studio 的版本是否晚于 2015,即完全支持 C++11 的 Visual Studio 的第一个版本(对于 Visual Studio Studio 2015,_MSC_VER
宏具有值1900
,请参阅此答案)。
回答by Vamshi Krishna
If you do not want to use Boost.Config and need to test for compilers that support C++11 then checking the value of the constant __cplusplus
will do. However, a compiler might support most of the popular features of the C++11 standard yet it does not support the entire specifications. If you want to enable support for specific Visual Studio compilers which are not yet 100% compliant to C++11 specifications then use the following code snippet which allows compiling in Visual Studio 2013:
如果您不想使用 Boost.Config 并且需要测试支持 C++11 的编译器,那么检查常量的值__cplusplus
就可以了。然而,编译器可能支持 C++11 标准的大多数流行特性,但它不支持整个规范。如果要启用对尚未 100% 符合 C++11 规范的特定 Visual Studio 编译器的支持,请使用以下允许在 Visual Studio 2013 中进行编译的代码片段:
#if defined(_MSC_VER)
# if _MSC_VER < 1800
# error This project needs atleast Visual Studio 2013
# endif
#elif __cplusplus <= 199711L
# error This project can only be compiled with a compiler that supports C++11
#endif
A complete list of versions of the compiler for Visual Studio is provided at How to Detect if I'm Compiling Code With Visual Studio 2008
如何检测我是否使用 Visual Studio 2008 编译代码中提供了 Visual Studio 编译器版本的完整列表
回答by diverscuba23
In the traditional Linux/Unix world, autoconf is traditionally used to test for the presence of libraries and compiler features and bugs placing them into a config.h that you use in your files as needed.
在传统的 Linux/Unix 世界中,autoconf 传统上用于测试库和编译器功能的存在以及将它们放入 config.h 中的错误,您可以根据需要在文件中使用它。
回答by yairchu
When your check is for a C++11 library availability (not a language feature), for example the <array>
header, you can #if __has_include(<array>)
.
当您检查 C++11 库可用性(不是语言功能)时,例如<array>
标题,您可以#if __has_include(<array>)
.
Sometimes checking #if __cplusplus >= 201103L
would tell you that you use C++11 but other settings like the standard library version setting in Xcode may still not have the new libraries available (most of them are available in different names ie <tr1/array>
)
有时检查#if __cplusplus >= 201103L
会告诉您您使用的是 C++11,但其他设置(例如 Xcode 中的标准库版本设置)可能仍然没有可用的新库(其中大多数都有不同的名称,即<tr1/array>
)