e1?(void_e2):(void_e3) so, is it legal or what?
Geoff Kuenning
geoff at desint.UUCP
Sun Aug 17 06:09:38 AEST 1986
In article <745 at wjvax.wjvax.UUCP> brett at wjvax.UUCP (Brett Galloway) writes:
> In article <243 at desint.UUCP> geoff at desint.UUCP (Geoff Kuenning) writes:
>>let us not forget that there
>>is *no* reason to ever write this particular expression.
> You are correct, but this is true of ALL uses of '?:`. In fact, '?:` is
> VERY useful, especially when you want to embed conditionals in a macro.
> Using the if() {} else {} form restricts the contexts in which the macro
> may appear.
Unfortunately, Brett is incorrect here. You can't use if/then/else to write:
for (i = up ? 0 : 99; up ? (i < 100) : (i >= 0); up ? i++ : i--)
{
/* complex loop body */
}
without duplicating code. On the other hand, anywhere you want do
if/then/else (or any other complex statement, such as loops and switches)
inside a macro without restricting where the code can be used, you can
just write:
#define abc(x) do { \
/* anything you want goes here, even declarations */ \
} while (0)
and almost all compilers will optimize the loop out completely
(something that very few optimizers could do with the "complex loop
body" above, especially because it trades time off for space and few
compiler writers are willing to make that decision). I learned this
method from in net.lang.c.
--
Geoff Kuenning
{hplabs,ihnp4}!trwrb!desint!geoff
More information about the Comp.lang.c
mailing list