我有一个函数说void foo()
。 我贬低是旧的function: –
void foo()__attribute__ ((deprecated));
新function: –
void FOO();
现在我想在旧函数中添加一条消息,说明“使用的新函数是FOO
”,它可以在编译代码后看到的警告消息一起看到。
这个怎么做。
你可以使用[[deprecated(msg)]]
属性,这也是一个标准的方法(自C ++ 14以来)。
[[deprecated("do not use")]] void f() {} int main(){ f(); }
clang++
输出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();
您可以在属性本身内指定消息(自GCC 4.5以来)
void __attribute__ ((deprecated("the new function used is FOO"))) foo();
或者,您可以使用新的语法(C ++ 14)
[[deprecated("the new function used is FOO")]] void foo();
如果您使用C ++ 14,则可以使用以下语法:
[[deprecated("Replaced by FOO, which has extra goodness")]] void foo();
请注意,您只能使用消息的字符串文字。