简体   繁体   中英

Special qualifiers for functions in C++

I have seen code in several places where you have a function qualified by a user defined keyword. For example

#define EXTERNAL_API dec __declspec(dllexport)

and then we have a function call

void EXTERNAL_API doStuff() {}

I understand the purpose in this case but I want to know if these qualifiers can be used to perform specific behavior on functions that are qualified with this. For example, if I want to mark a function as deprecated in my class and if I make use of this function call elsewhere, I get a compile time warning that this function is deprecated, use doStuff2() instead. How can I achieve this functionality?

Each compiler has its own keywords to mark function as deprecated, for MSVS you can do the following:

#define DEPRECATED __declspec(deprecated(COMPILE_ERROR_TEXT))

and you can use it as:

DEPRECATED void oldFunction();

In C++14 there is a possibility to use [[deprecated]] tag. Like that:

[[deprecated]]
void oldFunc();

I hope that will help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM