忽略的后果是:警告:未使用的参数 [英] What are the consequences of ignoring: warning: unused parameter

查看:165
本文介绍了忽略的后果是:警告:未使用的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在一个C ++项目上,我注意到我们有许多关于未使用参数的警告。

I am working on a C++ project and I noticed that we have a number of warnings about unused parameters.

如果忽略这些警告会产生什么影响?

What effect could it have if these warnings are ignored?

推荐答案

在以下情况下,带有未使用参数的函数可能会出现真正的错误:

The function with an unused parameter may have a real bug in the following cases:


  1. 有一个 output 参数,该参数未分配或写入,导致调用者的值未定义。

  1. There is an output parameter, which is not being assigned or written into, resulting in undefined value for the caller.

参数之一是回调函数指针,您必须调用它,而忘记这样做。如果函数中有很多 #ifdef ,则可能会发生。

One of parameters is a callback function pointer, which you must invoke and forget to do so. May happen if there is a lot of #ifdefs in the function.

您声明一个本地具有相同名称的变量会隐藏参数,并随后在函数中使用错误的值。

You declare a local variable with the same name that shadows a parameter and subsequently use the wrong value down in the function.

不使用 input 参数可能是无害的,但是您可以通过在函数的开头显式标记未使用的输入参数(将其强制转换为<$ c $)来减少噪声以查看有用的警告。 c> void (适用于C和C ++):

Not using an input parameters may be harmless, but you can reduce the noise to see useful warnings by marking unused input parameters explicitly in the beginning of the function by casting it to void (works for both C and C++):

(void)param1;

或者,

#define UNUSED(expr) do { (void)(expr); } while (0)
...

void foo(int param1, int param2)
{
    UNUSED(param2);
    bar(param1);
}

或忽略参数名称(仅C ++):

Or omit parameter name (C++ only):

void foo(int param1, int /*param2*/)
{
    bar(param1);
}

这篇关于忽略的后果是:警告:未使用的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆