__cplusplus<201402L即使在我指定-std = c ++ 14时在gcc中也返回true [英] __cplusplus < 201402L return true in gcc even when I specified -std=c++14
问题描述
指令:
#ifndef __cplusplus
#error C++ is required
#elif __cplusplus < 201402L
#error C++14 is required
#endif
命令行: g ++ -Wall -Wextra -std = c ++ 14 -c -o header.o header.hpp
我的g ++版本: g ++(tdm-1)4.9.2
即使我添加了 -std = c ++ 14
,也会生成错误 C ++ 14是必需的
,我不知道为什么.
The error C++14 is required
is generated even when I added -std=c++14
, I don't know why.
请告诉我如何解决此问题.
Please tell me how to fix this.
推荐答案
根据GCC CPP手册(版本 5.1.0 ):
According to the GCC CPP manual (version 4.9.2 and 5.1.0):
__ cplusplus
该宏是在使用C ++编译器时定义的.您可以使用__ cplusplus
来测试标头是由C编译器还是C ++编译器编译的.此宏类似于__ STDC_VERSION __
,因为它扩展为版本号.根据所选的语言标准,宏的值为199711L
,这是1998 C ++标准所规定的;201103L
,根据2011 C ++标准;对于由-std = c ++ 1y
和-std = gnu +启用的实验语言,一个严格大于
.201103L
的未指定值+ 1y
__cplusplus
This macro is defined when the C++ compiler is in use. You can use__cplusplus
to test whether a header is compiled by a C compiler or a C++ compiler. This macro is similar to__STDC_VERSION__
, in that it expands to a version number. Depending on the language standard selected, the value of the macro is199711L
, as mandated by the 1998 C++ standard;201103L
, per the 2011 C++ standard; an unspecified value strictly larger than201103L
for the experimental languages enabled by-std=c++1y
and-std=gnu++1y
.
您可以检查 g ++ --std = c ++ 14
是否将 __ cplusplus
定义为:
You can check that g++ --std=c++14
defines __cplusplus
as:
Version __cplusplus
4.8.3 201300L
4.9.2 201300L
5.1.0 201402L
对于 clang ++ --std = c ++ 14
:
Version __cplusplus
3.3 201305L
3.4 201305L
3.5.x 201402L
3.6 201402L
3.7 201402L
因此,更安全的检查应该是:
So a safer check should probably be:
#ifndef __cplusplus
# error C++ is required
#elif __cplusplus <= 201103L
# error C++14 is required
#endif
如注释中所述,这可能意味着对C ++ 14的部分支持.
As specified in the comment, this could mean partial C++14 support.
要检查特定功能,您还可以尝试 Boost Config (尤其是描述宏的宏不支持C ++ 14功能).
To check for a specific feature you could also try Boost Config (especially Macros that describe C++14 features not supported).
这篇关于__cplusplus<201402L即使在我指定-std = c ++ 14时在gcc中也返回true的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!