Archive for December, 2009
Creating unique variables within your preprocessor macros
Dec 1st
Sometimes in your macros you need to use a variable, but you can’t scope it to the macro. If you use this macro more than one in the same scope you get a duplicate variable warning.
For example, let’s say this is your macro:
#define LOOP_10_TIMES \ int i = 0; \ for( i = 0; i < 10; ++i)
And this is your implementation:
LOOP_10_TIMES{
x += i;
}
This is fine, but what happens when you want to use this macro twice? Like so:
LOOP_10_TIMES num += i; LOOP_10_TIMES mun += i;
You’re sure to get a nasty error (or at least a warning).
This is because your code now declares the variable ‘i’ twice in the same scope.


