How to add a line or message in a deprecated function

I have a say void function foo()

. I condemn this as old func: -

void foo()__attribute__ ((deprecated));

      

new func: -

void FOO();

      

Now I want to add a message along with this in the old function that "new function is in use FOO

", which can be seen along with the warning message we get after compiling the code.

How to do it.

+3


source to share


3 answers


You can use the attribute [[deprecated(msg)]]

, which is the standard way to do it (since C ++ 14).

[[deprecated("do not use")]]
void f()
{}

int main(){

f();
}

      

conclusion clang++

:



warning: 'f' is deprecated: do not use [-Wdeprecated-declarations]
f();
^
note: 'f' has been explicitly marked deprecated here
void f()
     ^
1 warning generated.

      

conclusion 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();

      

+3


source


You can specify the message inside the attribute itself (since GCC 4.5)

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

      



Alternatively, you can use the new syntax (C ++ 14)

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

      

+3


source


If you are using C ++ 14 you can use this syntax:

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

      

Note that only a string literal can be used for a message.

+1


source







All Articles