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


大气响应式网络建站服务公司织梦模板
高端大气html5设计公司网站源码
织梦dede网页模板下载素材销售下载站平台(带会员中心带筛选)
财税代理公司注册代理记账网站织梦模板(带手机端)
成人高考自考在职研究生教育机构网站源码(带手机端)
高端HTML5响应式企业集团通用类网站织梦模板(自适应手机端)