栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

c: error: this statement may fall through [-Werror=implicit-fallthrough=]

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

c: error: this statement may fall through [-Werror=implicit-fallthrough=]

文章目录
  • fallthrough
  • 原因
  • 实例代码
  • 解决方法;

fallthrough

就是掉落穿透,比如从楼上掉下来一个导弹,直接就fall through 整个楼层。

原因

这个是大家经过多年经验总结而来的一个警告,以防止疏忽大意导致代码里出现非程序员本意的代码逻辑来,缺少了break语句。逻辑,就是如果是要break,就要加上语句break;但是如果不加break,代表fallthrough,这个fallthrough不是每个人都能意识到,或者是忘记了,所以要明确写上是否要fallthrough。

实例代码

switch (cond)
{
case 1:
a = 1;
break;
case 2:
a = 2;/// 这一句的后面可能是少了break;如果在编译选项里加上Werror=implicit-fallthrough,给程序员一个提示。
case 3:
a = 3;
break;
}

解决方法;

如果是故意的,让代码fallthrough,就需要使用gcc提供的attribute:

attribute ((fallthrough)),
switch (cond)
{
case 1:
bar (0);
attribute ((fallthrough)); //在这里加上这个属性,就代表,我是故意这么做,不用报warning,错误。
default:

C++17 provides a standard way to suppress the ‘-Wimplicit-fallthrough’ warning using [[fallthrough]]; instead of the GNU attribute. In C++11 or C++14 users can use [[gnu::fallthrough]];, which is a GNU extension. Instead of the these attributes, it is also possible to add a fallthrough comment

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/935626.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号