<bdo id='Sq8UE'></bdo><ul id='Sq8UE'></ul>

<legend id='Sq8UE'><style id='Sq8UE'><dir id='Sq8UE'><q id='Sq8UE'></q></dir></style></legend>
  • <tfoot id='Sq8UE'></tfoot>
    <i id='Sq8UE'><tr id='Sq8UE'><dt id='Sq8UE'><q id='Sq8UE'><span id='Sq8UE'><b id='Sq8UE'><form id='Sq8UE'><ins id='Sq8UE'></ins><ul id='Sq8UE'></ul><sub id='Sq8UE'></sub></form><legend id='Sq8UE'></legend><bdo id='Sq8UE'><pre id='Sq8UE'><center id='Sq8UE'></center></pre></bdo></b><th id='Sq8UE'></th></span></q></dt></tr></i><div id='Sq8UE'><tfoot id='Sq8UE'></tfoot><dl id='Sq8UE'><fieldset id='Sq8UE'></fieldset></dl></div>

    1. <small id='Sq8UE'></small><noframes id='Sq8UE'>

        如果忽略返回值,如何发出警告?

        时间:2023-09-19
        1. <small id='GW1gV'></small><noframes id='GW1gV'>

              <i id='GW1gV'><tr id='GW1gV'><dt id='GW1gV'><q id='GW1gV'><span id='GW1gV'><b id='GW1gV'><form id='GW1gV'><ins id='GW1gV'></ins><ul id='GW1gV'></ul><sub id='GW1gV'></sub></form><legend id='GW1gV'></legend><bdo id='GW1gV'><pre id='GW1gV'><center id='GW1gV'></center></pre></bdo></b><th id='GW1gV'></th></span></q></dt></tr></i><div id='GW1gV'><tfoot id='GW1gV'></tfoot><dl id='GW1gV'><fieldset id='GW1gV'></fieldset></dl></div>
              <legend id='GW1gV'><style id='GW1gV'><dir id='GW1gV'><q id='GW1gV'></q></dir></style></legend>

                  <bdo id='GW1gV'></bdo><ul id='GW1gV'></ul>
                    <tbody id='GW1gV'></tbody>
                  <tfoot id='GW1gV'></tfoot>
                  本文介绍了如果忽略返回值,如何发出警告?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我想查看我的代码 (C++) 中所有忽略函数返回值的地方.我该怎么做 - 使用 gcc 或静态代码分析工具?

                  I'd like to see all the places in my code (C++) which disregard return value of a function. How can I do it - with gcc or static code analysis tool?

                  错误代码示例:

                  int f(int z) {
                      return z + (z*2) + z/3 + z*z + 23;
                  }
                  
                  
                  int main()
                  {
                    int i = 7;
                    f(i); ///// <<----- here I disregard the return value
                  
                    return 1;
                  }
                  

                  请注意:

                  • 即使函数及其用法在不同的文件中也应该可以工作
                  • 免费静态检查工具
                  • it should work even if the function and its use are in different files
                  • free static check tool

                  推荐答案

                  你想要 GCC 的 warn_unused_result 属性:

                  You want GCC's warn_unused_result attribute:

                  #define WARN_UNUSED __attribute__((warn_unused_result))
                  
                  int WARN_UNUSED f(int z) {
                      return z + (z*2) + z/3 + z*z + 23;
                  }
                  
                  int main()
                  {
                    int i = 7;
                    f(i); ///// <<----- here i disregard the return value
                    return 1;
                  }
                  

                  尝试编译此代码会产生:

                  Trying to compile this code produces:

                  $ gcc test.c
                  test.c: In function `main':
                  test.c:16: warning: ignoring return value of `f', declared with
                  attribute warn_unused_result
                  

                  您可以在 Linux内核;他们有一个 __must_check 宏可以做同样的事情;看起来你需要 GCC 3.4 或更高版本才能工作.然后你会发现内核头文件中使用的那个宏:

                  You can see this in use in the Linux kernel; they have a __must_check macro that does the same thing; looks like you need GCC 3.4 or greater for this to work. Then you will find that macro used in kernel header files:

                  unsigned long __must_check copy_to_user(void __user *to,
                                                          const void *from, unsigned long n);
                  

                  这篇关于如果忽略返回值,如何发出警告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:什么时候需要使用标志 -stdlib=libstdc++? 下一篇:启用 openmp 时出错 - “ld: library not found for -lgomp

                  相关文章

                  最新文章

                  <small id='aH4gV'></small><noframes id='aH4gV'>

                  <legend id='aH4gV'><style id='aH4gV'><dir id='aH4gV'><q id='aH4gV'></q></dir></style></legend>

                    • <bdo id='aH4gV'></bdo><ul id='aH4gV'></ul>

                      <tfoot id='aH4gV'></tfoot>
                      <i id='aH4gV'><tr id='aH4gV'><dt id='aH4gV'><q id='aH4gV'><span id='aH4gV'><b id='aH4gV'><form id='aH4gV'><ins id='aH4gV'></ins><ul id='aH4gV'></ul><sub id='aH4gV'></sub></form><legend id='aH4gV'></legend><bdo id='aH4gV'><pre id='aH4gV'><center id='aH4gV'></center></pre></bdo></b><th id='aH4gV'></th></span></q></dt></tr></i><div id='aH4gV'><tfoot id='aH4gV'></tfoot><dl id='aH4gV'><fieldset id='aH4gV'></fieldset></dl></div>