我有一个函数叫做void foo()
。我将其作为旧函数弃用:
void foo()__attribute__ ((deprecated));
新功能:
void FOO();
现在我想在旧函数中添加一条消息:"新函数使用的是
FOO
",这条消息可以与编译代码后得到的警告消息一起查看。如何实现此功能。
[[deprecated(msg)]]
属性,这也是一种标准的方式(自C++14起)实现此功能。[[deprecated("do not use")]]
void f()
{}
int main(){
f();
}
clang++
的输出结果:warning: 'f' is deprecated: do not use [-Wdeprecated-declarations]
f();
^
note: 'f' has been explicitly marked deprecated here
void f()
^
1 warning generated.
g++
的输出:
In function ‘int main()’:
warning: ‘void f()’ is deprecated (declared at test.cpp:2): do not use [-Wdeprecated-declarations]
f();
^
warning: ‘void f()’ is deprecated (declared at test.cpp:2): do not use [-Wdeprecated-declarations]
f();
void __attribute__ ((deprecated("the new function used is FOO"))) foo();
或者你可以使用新语法(C++14)
[[deprecated("the new function used is FOO")]]
void foo();
If you're using C++14, you can use this syntax:
[[deprecated("Replaced by FOO, which has extra goodness")]]
void foo();
-Wall
添加到编译器命令中? - Iharob Al Asimi