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


基础教程推荐
- C++结构和函数声明。为什么它不能编译? 2022-11-07
- 这个宏可以转换成函数吗? 2022-01-01
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- 我有静态或动态 boost 库吗? 2021-01-01
- 常量变量在标题中不起作用 2021-01-01
- 在 C++ 中计算滚动/移动平均值 2021-01-01
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
- 如何通过C程序打开命令提示符Cmd 2022-12-09
- 如何检查GTK+3.0中的小部件类型? 2022-11-30