[英]Ensure that g++ does not compile a program using features added in newer versions of C++
使用标志-std=c++14
编译的程序也会使用在较新版本的 C++ 中实现的功能,并发出如下警告:
warning: inline variables are only available with -std=c++17 or -std=gnu++17
我不希望 g++ 在这种情况下编译程序,并且不知道为什么会这样做。
我发现添加标志-Werror
会将上述警告转换为错误,确保程序无法编译,但我不确定这是否是推荐的方法。
为了引发编译器错误,专门用于使用仅在 C++ 标准中合法的语言功能,比您选择的标准晚,最佳针对性的诊断选项可能是-pedantic-errors
,已记录
-迂腐错误
每当基本标准(请参阅 -Wpedantic)需要诊断时给出错误,在某些情况下,在编译时存在未定义的行为,而在其他一些情况下,不会阻止编译根据标准有效的程序......
[我的重点]
这里的“基本标准”是 C++ 标准,由-std=...
的指定或默认值命名(或者如果该标准是像gnu++14
这样的 GNU 方言,那么它就是该方言所在的 C++ 标准基于)。
如果您使用std=c++14
编译源代码,该代码使用首先在 C++17 中合法化的构造,那么根据 C++14 标准,该代码格式不正确,并且需要进行诊断。 因此,将-pedantic-errors
添加到-std=c++14
将迫使编译器将 C++17 创新诊断为错误。
例如,没有-pedantic-errors
$ cat foo.cpp
struct foo
{
inline static const int value = 42;
};
$ g++ -std=c++14 -Wall -Wextra -pedantic -c foo.cpp
foo.cpp:3:5: warning: inline variables are only available with ‘-std=c++17’ or ‘-std=gnu++17’
3 | inline static const int value = 42;
| ^~~~~~
并带有-pedantic-errors
$ g++ -std=c++14 -Wall -Wextra -pedantic-errors -c foo.cpp
foo.cpp:3:5: error: inline variables are only available with ‘-std=c++17’ or ‘-std=gnu++17’
3 | inline static const int value = 42;
| ^~~~~~
-pedantic-errors
将使编译器对 C++14 的一致性比std-c++14
本身或-Werror
更加挑剔。 但我猜你不会 object 到那个。 您可以不受约束地选择是否还练习零警告编译的一揽子纪律( -Werror
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.