I have a simple code in header.h-
#define SWAP(a, b) {a ^= b; b ^= a; a ^= b;}
This header.h is included in a code.c file, but my requirement is- I want SWAP to be checked first like-
#ifndef SWAP(a, b)
#define SWAP(a, b) {a ^= b; b ^= a; a ^= b;}
#endif
Is this correct or I don't have to provide the argument to the first line?
You want to code
#ifndef SWAP
#define SWAP(a, b) {a ^= b; b ^= a; a ^= b;}
#endif
The #ifndef
is just looking into the preprocessor's symbol table (for the presence or absence of some given preprocessor symbol). It does not care about the arguments of your SWAP
macro.
Of course in genuine C++11 you should prefer the standard std::swap
function. It is typesafe, more readable, and safer (think about SWAP(t[i++],i)
as a misbehaving example).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With