Richard Heathfield said:
Philip Potter said:
No, not really - at least, I just tried three times to break his version
and failed each time. So I owe him an apology.
[...]
Nevertheless, I would certainly use parentheses myself:
#define MAX(a, b) ((a) > (b) ? (a) : (b))
rather than
#define MAX(a, b) ((a) > (b) ? a : (b))
Consistency is certainly part of it. Another part is that I'm *still*
not 100% convinced that the parentheses are unnecessary. I have a
nagging hunch that there *might* be an argument that would break the
first form, perhaps something involving the ?: operator in the operand
itself. Or perhaps there's some argument that should be a syntax
error, but that will cause the unparenthesized version to expand to a
legal expression that doesn't do what you want.
It's very likely that my concern is groundless, and that the
unparenthesized form is perfectly safe. But if it takes an
experienced C programmer like RH three tries, not to prove that it's
safe, but merely to fail to prove that it isn't, then it's likely to
waste some of the time of every attentive programmer who reads it. If
I'm tracking down a bug that manifests itself anywhere near any use of
this macro, I'm likely to waste time convincing myself that the macro
isn't the problem before persuing other possibilities.
We had something similar here recently, with a macro definition
something like:
#define N -42
Several people immediately complained that it *should* be
#define N (-42)
but it took a day or two before anyone was able to present an example
where it actually made a difference.