defining macros with a variable number of parameters

Started by
12 comments, last by vicviper 19 years, 10 months ago
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
Advertisement
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/
ASCII stupid question, get a stupid ANSI
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

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
"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

This topic is closed to new replies.

Advertisement