我有一个跨平台应用程序,在我的一些函数中,并没有使用传递给函数的所有值.因此我收到GCC的警告,告诉我有未使用的变量.
编码警告的最佳编码方式是什么?
围绕这个功能的#ifdef?
#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
{
Run Code Online (Sandbox Code Playgroud)
这太丑了,但似乎是编译器喜欢的方式.
或者我在函数末尾为变量赋值为零?(我讨厌它,因为它改变了程序流程中的某些东西以使编译器警告静音).
有正确的方法吗?
我在C++中有一小段代码:
#include <iostream>
#include <iterator>
#include <string>
using namespace std;
int main() {
int i=0;
istream_iterator<string> EOS;
double x;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
现在我用我的g ++(GCC)4.4.4编译它
g++ -W -Wall -pedantic test.cc -o test
Run Code Online (Sandbox Code Playgroud)
得到:
test.cc: In function 'int main()':
test.cc:9: warning: unused variable 'i'
test.cc:11: warning: unused variable 'x'
Run Code Online (Sandbox Code Playgroud)
为什么没有未使用的EOS警告?
目前,我使用以下函数模板来抑制未使用的变量警告:
template<typename T>
void
unused(T const &) {
/* Do nothing. */
}
Run Code Online (Sandbox Code Playgroud)
但是,当从Linux移植到cygwin时,我现在在g ++ 3.4.4上遇到编译器错误(在linux上我是3.4.6,所以这可能是一个bug修复?):
Write.cpp: In member function `void* Write::initReadWrite()':
Write.cpp:516: error: invalid initialization of reference of type 'const volatile bool&' from expression of type 'volatile bool'
../../src/common/Assert.h:27: error: in passing argument 1 of `void unused(const T&) [with T = volatile bool]'
make[1]: *** [ARCH.cygwin/release/Write.o] Error 1
Run Code Online (Sandbox Code Playgroud)
未使用的参数是一个声明为的成员变量:
volatile bool readWriteActivated;
Run Code Online (Sandbox Code Playgroud)
这是编译器错误还是我的代码中的错误?
这是最小的测试用例:
template<typename T>
void unused(T const &) { }
int main() {
volatile bool x = …Run Code Online (Sandbox Code Playgroud)