简体   繁体   中英

MACRO expansion to block of preprocessors

I have following preprocessor (code) block in my project before each function definition to unclutter the logging Macro.

#ifdef FC_NAME
#undef FC_NAME
#endif

#define FC_NAME  "myFunctionName"

But this itself looks kind of cluttered.

So, I am looking at replacing this with something that looks simpler and tried this

#define REDEF_FC_NAME(funcName) #ifdef FC_NAME \
\                                 #undef FC_NAME \
\                               #endif \
\                               #define FC_NAME funcName \

But this gives error saying macro def within a macro. So, is there a way of achieving the same effect?

The short answer is: you can't do that with macros.

But:

In C++, every function has a predefined __func__ variable.

GCC offers this as an extension to C, as I'm sure do many other compilers. On Windows, there appears to be the __FUNCTION__ macro (see http://msdn.microsoft.com/en-us/library/b0084kay(v=vs.71).aspx ).

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