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++:获取传递给函数的多维数组的行大小 2021-01-01
- 如何更改 SysDateTimePick32 或 CDateTimeCtrl 的背景颜色? 2022-01-01
- 提升 ASIO 流缓冲 2021-01-01
- 与 CAS 的原子交换(使用 gcc 同步内置函数) 2022-01-01
- c++ STL设置差异 2022-01-01
- 为什么我们不能使用“虚拟继承"?在 COM 中? 2022-01-01
- 如何在 C++ 中正确使用命名空间? 2022-01-01
- 随机插入/删除的综合向量与链表基准 2022-01-01
- 将不可复制的闭包对象传递给 std::function 参数 2021-01-01
- 如何部分禁用 cmake C/C++ 自定义编译器检查 2021-01-01
