__cplusplus < 201402L return 在 gcc 中为真,即使我指定了 -std=c++14

__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 is required,不知道为什么。

请告诉我如何解决这个问题。

根据 GCC CPP 手册(版本 4.9.2 and 5.1.0):

__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 is 199711L, as mandated by the 1998 C++ standard; 201103L, per the 2011 C++ standard; an unspecified value strictly larger than 201103L for the experimental languages enabled by -std=c++1y and -std=gnu++1y.

您可以检查 g++ --std=c++14 是否将 __cplusplus 定义为:

 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

所以更安全的检查应该是:

#ifndef __cplusplus
#  error C++ is required
#elif __cplusplus <= 201103L
#  error C++14 is required
#endif

如评论中所述,这可能意味着部分 C++14 支持。

要检查特定功能,您还可以尝试 Boost Config (especially Macros that describe C++14 features not supported)。