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
10 个解决方案
#1
53
Boost.Config has a plethora of macros that can be used to test for support for specific C++11 features.
提振。配置有很多宏,可以用来测试支持特定的c++ 11特性。
#2
98
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++库,并且进行了所有c++ 11的修改,他们是否会大胆地增加它。
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线程和标准的其他特定部分。
#3
29
As stated by the C++11 standard (§iso.16.8):
如上所述的c++ 11标准(§iso.16.8):
The name __cplusplus is defined to the value 201103L when 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特性的子集,我认为没有标准的、可移植的方法;您可以检查编译器文档或std库头文件以获得更多信息。
#4
24
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
#5
17
I just wrote a small test suite to check which C++11 features are supported by a specific compiler. However, this is of course a 'pre-compile-time' check.
我只是写了一个小测试套件来检查,c++ 11一个特定的编译器支持的功能。然而,这当然是一个“pre-compile-time”检查。
https://github.com/sloede/cxx11tests
https://github.com/sloede/cxx11tests
#6
17
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;
}
#7
9
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宏在201103 l,但任何版本的Visual Studio使它在199711 l的值用于其他编译器在c++ 11。这段代码比较_cplusplus宏对所有编译器除了Visual Studio 201103 l,如果编译器是Visual Studio,它检查如果版本的Visual Studio晚于2015年,第一个版本的Visual Studio完全支持c++ 11(Visual Studio 2015,_MSC_VER宏观价值1900,见这个答案)。
#8
7
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。配置和需要测试的编译器支持c++ 11然后检查__cplusplus恒定的值。然而,编译器可能支持最流行的c++ 11标准的特点却不支持整个规范。如果您想支持特定的Visual Studio编译器,而这些编译器还没有100%符合c++ 11规范,那么请使用以下代码片段,允许在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如何探测与Visual Studio 2008如果我编译代码
#9
5
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传统上被用来测试库和编译器特性和缺陷的存在将成一个配置。h,你用你的文件。
#10
1
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库可用性(不是语言特性)时,例如
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>
)
有时检查#如果__cplusplus > = 201103 l告诉你,你会使用C + + 11但其他设置标准库版本设置在Xcode中可能仍然没有新图书馆(他们中的大多数都有不同的名称即< tr1 /数组>)
#1
53
Boost.Config has a plethora of macros that can be used to test for support for specific C++11 features.
提振。配置有很多宏,可以用来测试支持特定的c++ 11特性。
#2
98
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++库,并且进行了所有c++ 11的修改,他们是否会大胆地增加它。
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线程和标准的其他特定部分。
#3
29
As stated by the C++11 standard (§iso.16.8):
如上所述的c++ 11标准(§iso.16.8):
The name __cplusplus is defined to the value 201103L when 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特性的子集,我认为没有标准的、可移植的方法;您可以检查编译器文档或std库头文件以获得更多信息。
#4
24
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
#5
17
I just wrote a small test suite to check which C++11 features are supported by a specific compiler. However, this is of course a 'pre-compile-time' check.
我只是写了一个小测试套件来检查,c++ 11一个特定的编译器支持的功能。然而,这当然是一个“pre-compile-time”检查。
https://github.com/sloede/cxx11tests
https://github.com/sloede/cxx11tests
#6
17
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;
}
#7
9
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宏在201103 l,但任何版本的Visual Studio使它在199711 l的值用于其他编译器在c++ 11。这段代码比较_cplusplus宏对所有编译器除了Visual Studio 201103 l,如果编译器是Visual Studio,它检查如果版本的Visual Studio晚于2015年,第一个版本的Visual Studio完全支持c++ 11(Visual Studio 2015,_MSC_VER宏观价值1900,见这个答案)。
#8
7
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。配置和需要测试的编译器支持c++ 11然后检查__cplusplus恒定的值。然而,编译器可能支持最流行的c++ 11标准的特点却不支持整个规范。如果您想支持特定的Visual Studio编译器,而这些编译器还没有100%符合c++ 11规范,那么请使用以下代码片段,允许在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如何探测与Visual Studio 2008如果我编译代码
#9
5
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传统上被用来测试库和编译器特性和缺陷的存在将成一个配置。h,你用你的文件。
#10
1
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库可用性(不是语言特性)时,例如
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>
)
有时检查#如果__cplusplus > = 201103 l告诉你,你会使用C + + 11但其他设置标准库版本设置在Xcode中可能仍然没有新图书馆(他们中的大多数都有不同的名称即< tr1 /数组>)