Yes - for operator precedence concerns in a macro body
While casually skimming the WebKit source, I found an example where the author intentionally reversed the operands to an array access.
#define ARRAY_SIZE(x) \
((sizeof(x)/sizeof(0[x])) / ((size_t)(!(sizeof(x) % sizeof(0[x])))))
Note that in two places, the author uses 0[x]
instead of the more traditional x[0]
. The reason for this, I assume, is that this usage happens inside a macro, rather than in raw code. Since x
is in fact a parameter to the macro, its value will be textually substituted into the macro body. A consequence of this is that x
does not have to be a single identifier, but perhaps in infix expression. In this case, there is potential for operator precedence issues to creep in. Take for example this usage:
int a[] = {0, 1};
ARRAY_SIZE(a); // Expands as 0[a]
int b[] = {0, 1};
ARRAY_SIZE(b+1); // Expands as 0[b+1]
If the operands were reversed, the two calls to ARRAY_SIZE
would expand to a[0]
and b+1[0]
respectively. The former works as intended, but the latter is a likely segmentation fault since it's equivalent to b+(1[0])
.
Effectively, by reversing the operands to the array access, the author has obviated the need to proactively parenthesize their macro parameters. This might not be an incredibly powerful usage, but it is a small benefit to reversing operands, at the (likely much greater) expensive of readability.