【发布时间】:2018-05-12 22:24:45
【问题描述】:
我的环境 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
我相信-Werror 选项导致了这个问题。但问题是什么?谁能告诉我?
#include <iostream>
int main() {
constexpr double yen_dollar = 0.107;
std::cout << yen_dollar << std::endl;
return 0;
}
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;
【问题讨论】:
-
很奇怪。 This online version of GCC 7.2 正在顺利编译它。你能提供更多细节吗?喜欢确切的命令行参数吗?
-
你能显示你用来构建源代码的 exact 命令行吗?将其复制(作为文本)并将其粘贴到问题的正文中,无需任何修改。
-
g++ -std=c++14 -O2 -Wall -Werror -Wextra -ansi -flto 谢谢
-
去掉
-ansi标志。