[英]How to assert that C++11 should be used to compile my program?
I want to use some C++11 features in my program. 我想在我的程序中使用一些C ++ 11功能。 I might have to share my source code with others in future.
我将来可能不得不与其他人分享我的源代码。 How do I assert, inside the code, that C++11 should be used to compile my program?
如何在代码中声明C ++ 11应该用于编译我的程序? An older compiler might throw an error, but I want the user to be informed clearly that C++11 is required.
较旧的编译器可能会抛出错误,但我希望用户能够清楚地知道需要C ++ 11。
I'm using the following C++11 features, if that matters: 我正在使用以下C ++ 11功能,如果这很重要:
thanks 谢谢
You could check that the value of the __cplusplus
macro is 201103L
or greater: 您可以检查
__cplusplus
宏的值是201103L
还是更高:
#if __cplusplus < 201103L
#error This code requires C++11
#endif
C++11 16.8 Predefined macro names: C ++ 11 16.8预定义的宏名称:
The following macro names shall be defined by the implementation:
以下宏名称应由实现定义:
__cplusplus
The name
__cplusplus
is defined to the value201103L
when compiling a C++ translation unit.编译C ++转换单元时,名称
__cplusplus
定义为值201103L
。 (155)(155)
(155) It is intended that future versions of this standard will replace the value of this macro with a greater value.
(155)本标准的未来版本将以更大的值替换此宏的值。 Non-conforming compilers should use a value with at most five decimal digits.
不合格的编译器应使用最多五位十进制数的值。
__cplusplus macro may come handy __cplusplus宏可能会派上用场
#if __cplusplus < 201103L
#error C++11 Required
#endif
Something like this 像这样的东西
As it has already been said, the correct solution would be to check for the __cplusplus
macro. 正如已经说过的那样,正确的解决方案是检查
__cplusplus
宏。 However, some compilers have a partial support for C++11 features but do not set this macro for the correct value. 但是,某些编译器部分支持C ++ 11功能,但不将此宏设置为正确的值。 For example, strongly-typed enumerations are available in g++ since GCC 4.4.0.
例如,自GCC 4.4.0以来,g ++中提供了强类型枚举。 However, with the option
-std=c++11
(and its equivalents), the macro __cplusplus
was not set to the good value before GCC 4.7.0 (it was set to 1 instead). 但是,使用选项
-std=c++11
(及其等价物),宏__cplusplus
未在GCC 4.7.0之前设置为良好值(而是设置为1)。 That means that some compilers can compile your code but won't if you check for C++11 that way. 这意味着某些编译器可以编译您的代码,但如果您以这种方式检查C ++ 11则不会。
If you just need specific features, then I would check for them with Boost.Config which defines a whole set of macros that can be used to check whther your compiler supports the required features. 如果您只需要特定的功能,那么我将使用Boost.Config检查它们,它定义了一整套宏 ,可用于检查编译器是否支持所需的功能。 In your case, you would need:
在您的情况下,您将需要:
BOOST_NO_CXX11_SCOPED_ENUMS
for strongly-typed enumerations. BOOST_NO_CXX11_SCOPED_ENUMS
用于强类型枚举。 BOOST_NO_CXX11_SMART_PTR
for std::shared_ptr
. std::shared_ptr
BOOST_NO_CXX11_SMART_PTR
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.