Archived

This topic is now archived and is closed to further replies.

Macro with variable args ?

This topic is 5092 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have a printf() like function which I use to print debug messages to a log file and to the debugger. Now, I want all calls to this func to be removed in the release mode. So what I have to do is check whether NDEBUG is defined and then #define the function call away. But, the function has a variable number of arguments, so how do I define a macro for it with multiple args ?? I have : void func(char* msg, ...); what I want to do : #ifdef DEBUG #define macro(X,Y) func(X,Y) #else #define macro(X,Y) #endif But func can have any number of args, not only two. So what do i do?

Share this post


Link to post
Share on other sites
You can''t have variable numbers of arguments for a macro.

If you are using MSVC, check out how MFC does their TRACE macro. The answer is not to have ANY brackets or a parameter list on the macro.

... just checked, they do it like this:

#ifdef _DEBUG
#define TRACE ::AfxTrace
#else
#define TRACE 1 ? (void)0 : ::AfxTrace
#endif

Here, ::AfxTrace is a function which is very similar to :rintf

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

Share this post


Link to post
Share on other sites
Simple answer: You cannot have a C preprocessor macro with a variable number of arguments.

Complex answer: The upcoming C standard, if I recall correctly, does provide variable argument macros. I expect most compilers have not extended themselves to this feature at this time. Perhaps GNU, though.

---- --- -- -
Blue programmer needs food badly. Blue programmer is about to die!

Share this post


Link to post
Share on other sites
quote:
Original post by mossmoss
The upcoming C standard,


Upcoming? After C99?



When I look upon seamen, men of physical science, and philosophers, man is the wisest of all beings. When I look upon priests, prophets, and interpreters of dreams, nothing is so contemptible as man.


  Diogenes

Share this post


Link to post
Share on other sites
Thanks Everybody !
Paradigm Shifter : I saw that technique in the "Using ASSERT,VERIFY,TRACE..." article on the front page.I read it just after posting in the forum.
Guess thats what I will do too.
Thanks.

Share this post


Link to post
Share on other sites
Hi all. I ran into the very same problem while trying to implement variable params in a macro call.

I guess C99 allows you to do it but...

Unfortunately MS VC 6.0 does not implement C99 and the service pack doesn''t fix that at all.

Does anyone know if MS VC 7.0 (.net?) implements C99 or whether it allows variable argument lists in macros?

Basically if it will compile the following:

#define LOG(...) \
{ \
LogText(__FILE__, __LINE__, __VA_ARGS__); \
}

I think I can do it this way:
#define LOG(params) \
{ \
LogText params \
}

where a LOG() call would look like this:
LOG((__FILE__, __LINE__, "My age is %d.\n", myAge));

But I would like to avoid having double parentheses everywhere as well as __FILE__, __LINE__, since that was the whole point of wrapping the function call with a macro.

I know someone is going to say use gcc or something other than MS VC and I am considering that but I''d like to know if MS VC 7 will do what I want.

Share this post


Link to post
Share on other sites