g ++编译选项-std = c ++ 14编译错误显示Werror = c ++ 11-compat

B.H*_*ong 1 c++ gcc c++11 c++14

我的环境Arch Linux,gcc 7.2

我正在学习C++并且我使用关键字constexpr来定义一个常量,而在编译时,它会给我一个错误消息
错误:identifier ‘constexpr’ is a keyword in C++11 [-Werror=c++11-compat]

我可以使用默认的g ++编译我的程序,但无法使用-std = c ++ 14和-Werror进行编译

我正在使用的命令是:

g++ -std=c++14 -O2 -Wall -Werror -Wextra -ansi -flto
Run Code Online (Sandbox Code Playgroud)

我认为该-Werror选项导致了这个问题.但是问题是什么?有人可以告诉我吗?

#include <iostream>

int main() {
    constexpr double yen_dollar = 0.107;
    std::cout << yen_dollar << std::endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)
test.cpp:4:5: error: identifier ‘constexpr’ is a keyword in C++11 [-Werror=c++11-compat]
     constexpr double yen_dollar = 0.107;
     ^~~~~~~~~
test.cpp: In function ‘int main()’:
test.cpp:4:5: error: ‘constexpr’ was not declared in this scope
test.cpp:5:16: error: ‘yen_dollar’ was not declared in this scope
     std::cout << yen_dollar << std::endl;
Run Code Online (Sandbox Code Playgroud)

YSC*_*YSC 6

从GCC文档§3.4控制C语言的选项,可以阅读:

-ansi

  In C mode, this is equivalent to -std=c90. In C++ mode, it is equivalent to -std=c++98. 
Run Code Online (Sandbox Code Playgroud)

既然,你编译了

g++ -std=c++14 -O2 -Wall -Werror -Wextra -ansi -flto
Run Code Online (Sandbox Code Playgroud)

-ansi覆盖-std=c++14-std=c++98.这就是为什么constexpr不被承认.

解决方案:摆脱-ansi旗帜.