问题描述
我有一个跨平台应用程序,在我的一些函数中,并非所有传递给函数的值都被利用.因此我收到 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.
或者我是否在函数末尾为变量赋值为零?(我讨厌它,因为它改变了程序流程中的某些内容以消除编译器警告).
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);
}
这篇关于如何最好地消除有关未使用变量的警告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!