如何在不推荐使用的函数中添加字符串或消息

时间:2015-01-06 13:28:08

标签: c++ c linux

我有一个函数说空foo()。我贬低就是 老功夫: -

void foo()__attribute__ ((deprecated));

new func: -

void FOO();

现在我想在旧函数中添加一条消息,“使用的新函数是FOO”,可以看到编译代码后我们将得到的警告消息。

如何做到这一点。

3 个答案:

答案 0 :(得分:3)

您可以使用[[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();

答案 1 :(得分:3)

您可以在属性本身内指定消息(自GCC 4.5起)

void __attribute__ ((deprecated("the new function used is FOO"))) foo();

或者,您可以使用新语法(C ++ 14)

[[deprecated("the new function used is FOO")]]
void foo();

答案 2 :(得分:1)

如果您正在使用C ++ 14,则可以使用以下语法:

[[deprecated("Replaced by FOO, which has extra goodness")]]
void foo();

请注意,您只能使用字符串文字作为邮件。