歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> C++裡消除Wunused

C++裡消除Wunused

日期:2017/3/1 9:30:25   编辑:Linux編程

編譯程序時,有一大堆警告總是不爽的。別人的代碼也就忍了,不好去改。自己的可沒法忍。看看C++裡怎麼消除Wunused警告。

先來看下面的程序:

#include <iostream>


int main(int argc,char **argv)
{
int a;

return 0;
}

編譯的時候,打開所有的警告:

[email protected]:~/code/test$ g++ -g -W -Wall -o unused unused.c
unused.c: In function ‘int main(int, char**)’:
unused.c:19:10: warning: unused variable ‘a’ [-Wunused-variable]
int a;
^
unused.c: At global scope:
unused.c:17:5: warning: unused parameter ‘argc’ [-Wunused-parameter]
int main(int argc,char **argv)
^
unused.c:17:5: warning: unused parameter ‘argv’ [-Wunused-parameter]

可以看到,沒有使用的變量、參數都給出了警告。警告是編譯器給的,我們當然可以把編譯選項中的-W -Wall去掉一了百了。可通常我們只是想告訴編譯器:這個地方是我故意這樣寫的,不用警告我。其他地方有問題才警告我。那麼就來看看編譯器的__attribute__((unused))屬性。這個屬性可以在聲明變量時指定變量是不需要用到的,不需要警告。如:

#include <iostream>

int main(int argc,char **argv)
{
int a __attribute__((unused));

return 0;
}

編譯時就不會再提示變量a未使用:

[email protected]:~/code/test$ g++ -g -W -Wall -o unused unused.c
unused.c:17:5: warning: unused parameter ‘argc’ [-Wunused-parameter]
int main(int argc,char **argv)
^
unused.c:17:5: warning: unused parameter ‘argv’ [-Wunused-parameter]

對__attribute__((unused))稍微包裝一下,就有了網上廣為流傳的版本:

#ifdef UNUSED
#elif defined(__GNUC__)
# define UNUSED(x) UNUSED_ ## x __attribute__((unused))
#elif defined(__LCLINT__)
# define UNUSED(x) /*@unused@*/ x
#else
# define UNUSED(x) x
#endif

#include <iostream>


int main(int UNUSED(argc),char **UNUSED(argv))
{
int UNUSED(a);

return 0;
}

這樣編譯時就完全沒有警告了。不過,如果有人沒注意看你的代碼,又使用變量a,則會提示error: ‘a’ was not declared in this scope。

上面的方法是GNUC裡提供的,而Qt裡有一個Q_UNUSED,它的實現在qglobal.h裡。

#ifndef Q_TYPENAME
# define Q_TYPENAME typename
#endif

//
// Use to avoid "unused parameter" warnings
//
#define Q_UNUSED(x) (void)x;

//
// Debugging and error handling
//

#if !defined(QT_NO_CHECK)
# define QT_CHECK_STATE // check state of objects etc.
# define QT_CHECK_RANGE // check range of indexes etc.
# define QT_CHECK_NULL // check null pointers
# define QT_CHECK_MATH // check math functions
#endif

當然,我們也可以按它的方式去做,不過它是在使用的時候(聲明一個int (void)a可以編譯通過,在函數的參數裡則不行),而GNUC方法是在聲明的時候。按Qt的方法:

#define UNUSED(x) (void)x

#include <iostream>


int main(int argc,char **argv)
{
int a;

UNUSED(a);
UNUSED(argc);
UNUSED(argv);

return 0;
}

這樣編譯也是沒有警告的。

上面兩種方法處理後,用 g++ -S unused.c生成匯編代碼來對比,與下面的程序是一致的,不會生成多余的代碼,沒有效率的問題:

#include <iostream>

int main()
{
int a;
return 0;
}

Copyright © Linux教程網 All Rights Reserved