【发布时间】:2018-08-23 10:21:07
【问题描述】:
我在 GCC 编译器版本 7.1.0 上测试 C++17 功能。
这与fallthrough 属性有关,以下示例(live example) 改编自在线CPP 参考here
#include "iostream"
using namespace std;
int f(int n) {
switch (n) {
case 1:
case 2:
n = n + 20;
[[fallthrough]];
case 3: // no warning on fallthrough
n = n + 30;
case 4: // compiler may warn on fallthrough
[[fallthrough]]; // illformed, not before a case label
//n = n + 40; //commented out to test if compiler will warn.
}
return n;
}
int main()
{
cout << f(1) << endl;
cout << f(2) << endl;
cout << f(3) << endl;
cout << f(4) << endl;
return 0;
}
最后一个[[fallthrough]](对于case 4:)格式不正确。
关于“根据标准,对格式不正确的程序需要什么 C++ 编译器?”的问题here 顶部有 answer 声明:
总结一下:如果一个格式错误的程序包含一个可诊断的违规,而标准没有明确指定“不需要诊断”,那么符合要求的实现应该发出一个诊断。
因此,我查看了标准 (N4713) 以查看它是否声明此问题不需要诊断。我找不到任何这样的声明。
有趣的是,毕竟,当我在最后一个[[fallthrough]]之后添加以下语句时
n = n + 40;
编译器警告(live example):
警告:属性“fallthrough”不在案例标签或默认标签之前
所以,这里有两个问题:
- 编译器是否错过了发出诊断,或者我是 这里缺少什么?
- 如果是编译器问题,是否严重到需要报告?
【问题讨论】:
-
@0xbaadf00d 我非常怀疑,standard 和 cppreference 都使用几乎完全相同的示例(即末尾的
[[fallthrough]])作为格式错误的构造。我想说这不是fallthrough问题,而是处理格式错误的构造。可以将格式错误的示例从标准复制粘贴到 gcc 中,并且可以干净地编译:godbolt.org/z/fftSM-. -
是的,我同意。这就是为什么我在您发布您的评论之前删除了我的评论。
-
顺便说一句,这个问题提示了这个错误报告gcc.gnu.org/bugzilla/show_bug.cgi?id=87068
-
附带说明,我建议使用 gcc >=7.2,7.1 包含太多错误并且不够稳定
标签: c++ gcc c++17 gcc-warning fall-through