确保 g++ 不会使用较新版本的 C++ 中添加的功能来编译程序
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
,也就是documented
-pedantic-errors
Give an error whenever the base standard (see -Wpedantic) requires a diagnostic, in some cases where there is undefined behavior at compile-time and in some other cases that do not prevent compilation of programs that are valid according to the standard...
[我的重点]
这里的“基本标准”是以-std=...
的指定值或默认值命名的C++标准(或者如果那个是GNU方言,如gnu++14
,那么它就是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
一起使用。但我想你不会反对的。你可以做一个不受约束的
关于您是否还实践零警告编译的一揽子纪律的选择 (-Werror
)
使用标志 -std=c++14
进行编译也会编译使用较新版本的 C++ 中实现的功能的程序,并发出如下警告:
warning: inline variables are only available with -std=c++17 or -std=gnu++17
我不希望 g++ 在这种情况下编译程序,一开始也不知道为什么会这样。
我发现添加标志 -Werror
会将上述警告转换为错误,确保程序无法编译,但我不确定这是否是推荐的方法。
专门为使用仅合法的语言功能引发编译器错误
在比您选择的标准晚的 C++ 标准中,最有针对性的诊断选项
大概是-pedantic-errors
,也就是documented
-pedantic-errors
Give an error whenever the base standard (see -Wpedantic) requires a diagnostic, in some cases where there is undefined behavior at compile-time and in some other cases that do not prevent compilation of programs that are valid according to the standard...
[我的重点]
这里的“基本标准”是以-std=...
的指定值或默认值命名的C++标准(或者如果那个是GNU方言,如gnu++14
,那么它就是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
一起使用。但我想你不会反对的。你可以做一个不受约束的
关于您是否还实践零警告编译的一揽子纪律的选择 (-Werror
)