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

标签 c++ gcc g++ c++14 predefined-macro

指令:

#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.25.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 (尤其是 Macros that describe C++14 features not supported )。

关于c++ - __cplusplus < 201402L 在 gcc 中返回真,即使我指定了 -std=c++14,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30995705/

相关文章:

c++ - 使用 g++ 和 Makefile 编译 c++ 时可执行文件的大小非常大

c++ - C++中 vector 下标超出范围,找不到错误源

c++ - 获取此对象的原始类型

c++ - 如何将应用程序与静态库链接+为什么这不起作用

android - 如何在 GCC 中使用来自不同路径的 C 库

deployment - netbeans c++ 部署

c++ - 更改调用约定

c++ - 从模板化父类中的派生内部类访问 protected 成员变量

c++ - 在 C 和 C++ 中将 char 转换为 int

c++ - 带有无符号整数的奇怪 gcc 行为