• Advertisement

Archived

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

defining macros with a variable number of parameters

This topic is 5003 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

hi! I usually define macros this way: #define mymacro(x,y) myfunction(x,y) and this works smooth... but, how can i do this: #define myprintf( ??? ) printf( ??? ) en then, use the myprintf macro with a different number of parameters ? like this: myprintf("hello world"); myprintf("%d %d",x,y); is this possible? is there any other alternative to do this?

Share this post


Link to post
Share on other sites
Advertisement
Whether it is possible or not depends entirely on your compiler, as it would be a language extension. Check your documentation.

For custom printf functions, check out vprintf.


“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.”
— Brian W. Kernighan

Share this post


Link to post
Share on other sites
C99 introduced variable argument function macros. With those, you can put an ellipses in the macro declaration to denote the use of variable number of arguments, and access the arguments with __VA_ARGS__. One example of a such a macro would be:

#define DEBUGF(f,...) \
(fprintf(dbgf, "%s(): ", f), fprintf(dbgf, __VA_ARGS__))


Of course if you''re not working with C99, then you would need to hope for a vendor specific preprocessor extension.

Share this post


Link to post
Share on other sites
It is not possible I''m afraid, the only way would be to use a function call with the ... in the parameters

Share this post


Link to post
Share on other sites
Yes I should say my answer applies to Visual Studio only.

Share this post


Link to post
Share on other sites
quote:
Original post by Trip99
It is not possible I''m afraid, the only way would be to use a function call with the ... in the parameters

...

Yes I should say my answer applies to Visual Studio only.



Variadic functions are standard C, not VS-specific.


“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.”
— Brian W. Kernighan

Share this post


Link to post
Share on other sites
if you use
void foo(...)
{ }
is there some way to acess the arguments one by one or do you have to deal with the whole chunk?

Share this post


Link to post
Share on other sites
It can be done, sort of:

#define MY_PRINTF(args) printf args

and to use it:

MY_PRINTF (("format_string", arg1, arg2));

Note the double parenthesis and the lack of parenthese around the ''args'' in the macro expansion. It''s a bit of a hack and the compiler will barf when you forget to put the ''(('' or ''))'' in.

If you''re using C++, you can use the << operator instead:

#define MY_PRINTF(args) printf_object << args

MY_PRINTF ("string" << arg1 << "string" << arg2);

which is a bit better.

Skizz

Share this post


Link to post
Share on other sites
#define myprintf printf

Just miss the brackets off the macro. This topic comes up all the time.

"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
What if you wanted to do something like:


#define sum( total, var ) total += var;


What I am trying to do is have the compiler run that line for every var value it gets. This example is just that, an example, and more useful applications for this macro are also more complex.

Is there any way to have a macro that ''unrolls'' itself?

Thanks

Dwiel

Go see some hot chicks and vote to see who is hottest: dwiel.no-ip.com

Share this post


Link to post
Share on other sites
Bah it is too possible, but a bit impractical. You have to create a class with two member variables and you need 3 macros to do it. This is an awsome article that shows how to make a really awsome new assert macro using this trick. http://www.cuj.com/documents/s=8464/cujcexp0308alexandr/

Share this post


Link to post
Share on other sites
quote:
Original post by Paradigm Shifter
#define myprintf printf

Just miss the brackets off the macro. This topic comes up all the time.


Err, that''s a bit flawed, especially if you want to implement a log system that is only present in a debug build for example:

#if defined _DEBUG
#define myprintf printf
#else
#define myprintf // what exactly? this will cause compiler errors
#endif

Skizz

Share this post


Link to post
Share on other sites
Then just do

#define myprintf nullprintf

where

void nullprintf(...) {}

is a function that eats all its arguments in a release build.

Another alternative is to use

#define myprintf 1 ? (void) 0 : printf

like TRACE in MFC for a release build.

"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

  • Advertisement