当我想念[[[Fallthrough]]]时
我正在使用GCC版本8.3,并具有开关语句:
#include <iostream>
enum class Type : char
{
Value1 = 'A',
Value2,
Value3,
Value4,
};
int main()
{
Type t;
switch(t)
{
case Type::Value1:
case Type::Value2:
case Type::Value3:
std::cout << "hw" << std::endl;
case Type::Value4:
break;
}
}
我想被迫使用 [[Fallthrough]]
,否则会生成警告。
我添加了GCC标志来检测缺失的枚举,并暗示了跌落的量:
g++ -o main.cc -O3 -Wswitch-enum -Wimplicit-fallthrough -std=c++1z
已经检测到新标志,因为我现在收到警告,我缺少枚举。我修理了这些,但仍然没有收到有关隐式秋天的警告。
代码放在此处:
https://godbolt.org/z/rj5stpwz4
我已经将 某物?我预计会被警告吗?
更新:
我更改了代码:
struct Object
{
int a;
};
int main()
{
Type t;
Object o;
switch(t)
{
case Type::Value1:
case Type::Value2:
case Type::Value3:
case Type::Value4:
o.a = 5;
break;
}
}
它仍然不会产生警告。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为,如果您之间的标签之间没有陈述,那就不会被认为是秋天的,这只是声明中的多个案例标签。
GCC甚至没有警告
(与Clang不同),但它确实在
因此,似乎您似乎需要实际的摔倒(在GCC和Clang上),并陷入有效的东西以引起警告(在GCC上)。
I don't think it's considered fallthrough if you've got no statements in between the labels—that's just multiple case labels on a statement.
Gcc doesn't even warn on
though (unlike clang), but it does warn on
https://godbolt.org/z/s8Ka84Gq6
so it seems you need both actual fallthrough (on both gcc and clang) and into something that has an effect in order to elicit the warning (on gcc).
因为只是在那里破裂,所以无论您是否跌倒,都没有区别。在那里添加一些东西。
Because there is just break there, there is no difference, if you fall through or not. Add something there.