P
Peter Ammon
Let's say I need to swap two int values frequently. I would write a macro:
#define swap(int a, int b) \
do { \
int temp = (a); \
(a) = (b); \
(b) = temp; \
} while (0)
Aside from the double evaluation of a and b, this works fine *except*
when either macro parameter a or b is called "temp." What's the best to
design macros to avoid these sort of collisions?
Is this a good candiate for using an identifier from that murky
legal-only-as-a-local-variable set, like _temp?
Thanks for your thoughts,
-Peter
(PS Yes, I know about various tricks with bitwise XOR, but I'm
interested in a general solution)
#define swap(int a, int b) \
do { \
int temp = (a); \
(a) = (b); \
(b) = temp; \
} while (0)
Aside from the double evaluation of a and b, this works fine *except*
when either macro parameter a or b is called "temp." What's the best to
design macros to avoid these sort of collisions?
Is this a good candiate for using an identifier from that murky
legal-only-as-a-local-variable set, like _temp?
Thanks for your thoughts,
-Peter
(PS Yes, I know about various tricks with bitwise XOR, but I'm
interested in a general solution)