MACRO expansion to block of preprocessors

Go To StackoverFlow.com

0

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?

2012-04-03 23:46
by Microkernel
Seems like you want a pre-pre-processor... I don't think you can do that in C/C++ - Mysticial 2012-04-03 23:48
You could include another file (or even yourself) conditionally - Kerrek SB 2012-04-03 23:49
@KerrekSB I didn't get you - Microkernel 2012-04-03 23:52
@Mysticial yeah, something like that : - Microkernel 2012-04-03 23:52
@Mysticial: m4 is your friend. (Kind of. - Mike Sherrill 'Cat Recall' 2012-04-03 23:52
What's wrong with simply undefining FC_NAME unconditionally - Chris 2012-04-03 23:52
Hm, never mind, I think you can't make a macro that produces another macro definition. But check out Boost.preprocessor for some neat tricks - Kerrek SB 2012-04-03 23:53
@all, guys, got the answer, __FUNCTION__ is defined in visual studio and __func__ in gcc : - Microkernel 2012-04-03 23:56


2

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).

2012-04-03 23:48
by Oliver Charlesworth
I want it on Windows (Visual C++) : - Microkernel 2012-04-03 23:51
@Microkernel: Then you should mention that in your question - Oliver Charlesworth 2012-04-03 23:53
Cool, Thanks : - Microkernel 2012-04-04 00:01
Useless fact: __func__ is actually a function-local variable, not a macro - James McNellis 2012-04-04 00:03
@JamesMcNellis: Useless, but interesting. Thanks - Oliver Charlesworth 2012-04-04 07:36
Ads