码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 解决warning: this statement may fall through [-Wimplicit-fallthrough=]


    使用switch如果缺少break,gcc编译的时候会报相关的warnning信息,如果是忘记写,这样肯定是有问题的,警告信息可以帮助我们排除隐藏的bug。要消除警告很简单,把break加上就行。

    但是有时候,我们的需求就是需要继续向下执行,是故意为之,那么这种warnning应该怎么消除呢?我们需要告诉编译器我们是故意这样写的,让它忽略该问题。

    1. #ifndef __has_cpp_attribute
    2. #define __has_cpp_attribute(x) 0
    3. #endif
    4. #ifndef __has_c_attribute
    5. #define __has_c_attribute(x) 0
    6. #endif
    7. #ifndef __has_attribute
    8. #define __has_attribute(x) 0
    9. #endif
    10. #if __has_cpp_attribute(fallthrough) || __has_c_attribute(fallthrough)
    11. #define FALLTHROUGH [[fallthrough]]
    12. #elif __has_attribute(fallthrough)
    13. #define FALLTHROUGH __attribute__((fallthrough))
    14. // Note, there could be more branches here, like using `[[gsl::suppress]]` for MSVC
    15. #else
    16. #define FALLTHROUGH
    17. #endif
    18. // example
    19. void example(int cond) {
    20. switch (cond) {
    21. case 0:
    22. dosomething();
    23. FALLTHROUGH;
    24. case 1:
    25. myfun();
    26. break;
    27. default:
    28. break;
    29. }
    30. }

    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 

  • 相关阅读:
    Github或Gitlab等申请PR或MR(pull request & merge request)的时候如果不能自动合并代码发生冲突了要怎么办
    数据结构与算法 - 树
    python多进程编程
    25李沐动手学深度学习v2/填充和步幅
    MySQL数据库(四)
    基于某评论的TF-IDF下的LDA主题模型分析
    python基于GCN(图卷积神经网络模型)和LSTM(长短期记忆神经网络模型)开发构建污染物时间序列预测模型
    安卓开发调用摄像头问题
    洛谷-P1106-删数问题
    【云原生之k8s】Helm 工具安装
  • 原文地址:https://blog.csdn.net/ganwenbo2011/article/details/127898995
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号