如何最好保持关于未使用变量的警告? [英] How do I best silence a warning about unused variables?

查看:272
本文介绍了如何最好保持关于未使用变量的警告?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个跨平台的应用程序,在我的一些功能不是所有的值传递到函数都被利用。因此,我从GCC收到警告,告诉我有未使用的变量。

I have a cross platform application and in a few of my functions not all the values passed to functions are utilised. Hence I get a warning from GCC telling me that there are unused variables.

对警告编码的最好方法是什么?

What would be the best way of coding around the warning?

一个#ifdef的函数?

An #ifdef around the function?

#ifdef _MSC_VER
void ProcessOps::sendToExternalApp(QString sAppName, QString sImagePath, qreal qrLeft, qreal qrTop, qreal qrWidth, qreal qrHeight)
#else
void ProcessOps::sendToExternalApp(QString sAppName, QString sImagePath, qreal /*qrLeft*/, qreal /*qrTop*/, qreal /*qrWidth*/, qreal /*qrHeight*/)
#endif
{

看起来像编译器喜欢的方式。

This is so ugly but seems like the way the compiler would prefer.

或者在函数结束时为变量赋值0? (我讨厌,因为它改变了程序流程中的一些东西,以沉默编译器警告)。

Or do I assign zero to the variable at the end of the function? (which I hate because it's altering something in the program flow to silence a compiler warning).

有正确的方法吗?

推荐答案

您可以将它放在(void)var; 表达式中(无效),以便编译器看到它被使用。这是在编译器之间可移植的。

You can put it in "(void)var;" expression (does nothing) so that a compiler sees it is used. This is portable between compilers.

例如

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

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

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

这篇关于如何最好保持关于未使用变量的警告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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