實施棄用警告的一種方式是針對不推薦使用的函數調用產生警告,除非您是從已棄用的上下文中調用。通過這種方式,傳統代碼可以調用遺留代碼,而不會產生相當於噪聲的警告。如何擺脫GCC中不推薦使用的函數中的棄用警告?
這是一個合理的思路,它體現在OS X上的GCC 4.2(1)和Clang 4.0(2)以及Ubuntu上的Clang 3.0(3)的實現中。
- (1):爲i686-蘋果darwin11-LLVM-G ++ - 4.2(GCC)4.2.1(基於蘋果公司建立5658)(LLVM建立2336.11.00)
- (2):蘋果鏗鏘版4.0(tags/Apple/clang-421.0.57)(基於LLVM 3.1svn)
- (3):Ubuntu clang 3.0-6ubuntu3(tags/RELEASE_30/final)(基於LLVM 3.0)
但是,當我在Ubuntu上使用GCC 4.6(4)進行編譯時,我對deprecat的所有調用都獲得了不推薦的警告獨立於上下文的編輯功能。這是功能的迴歸?有沒有我可以用來獲得其他行爲的編譯器選項?
- (4):克++(Ubuntu的/ Linaro的4.6.3-1ubuntu5)4.6.3
實施例的程序:從GCC 4.2
int __attribute__((deprecated)) a() {
return 10;
}
int __attribute__((deprecated)) b() {
return a() * 2; //< I want to get rid of warnings from this line
}
int main() {
return b(); //< I expect a warning on this line only
}
輸出(是的,我確實得到了同樣的警告兩次,但我不在乎):
main.cpp: In function ‘int main()’:
main.cpp:10: warning: ‘b’ is deprecated (declared at main.cpp:5)
main.cpp:10: warning: ‘b’ is deprecated (declared at main.cpp:5)
從GCC 4.
輸出:
main.cpp: In function 'int b()':
main.cpp:6:9: warning: 'int a()' is deprecated (declared at main.cpp:1) [-Wdeprecated-declarations]
main.cpp:6:11: warning: 'int a()' is deprecated (declared at main.cpp:1) [-Wdeprecated-declarations]
main.cpp: In function 'int main()':
main.cpp:10:9: warning: 'int b()' is deprecated (declared at main.cpp:5) [-Wdeprecated-declarations]
main.cpp:10:11: warning: 'int b()' is deprecated (declared at main.cpp:5) [-Wdeprecated-declarations]
我怎麼能說服GCC 4.6,它應該給我的輸出作爲GCC 4.2一樣嗎?
這是完全有可能的,這從來沒有與FSF GCC的工作,使4.2的行爲,你所看到的是蘋果的補丁GCC。你有沒有在任何地方安裝FSF GCC 4.2? – hvd
@ hvd你說得對。我想看看那個測試,但我手頭沒有4.2:/ –