e1?(void_e2):(void_e3) so, is it legal or what?
Walter Bright
bright at dataio.UUCP
Thu Aug 14 02:29:57 AEST 1986
In article <243 at desint.UUCP> geoff at desint.UUCP (Geoff Kuenning) writes:
>While all of this discussion is very interesting and is important to
>achieving a precise language definition, let us not forget that there
>is *no* reason to ever write this particular expression. Anywhere it's
>useful, you can just write
>
> if (e1)
> void_e2;
> else
> void_e3;
Not quite. This type of expression is very useful for macros which must
in themselves be expressions (to avoid peculiar {} and ; problems). Such
as:
#define biff(pow) ((pow) ? kayo() : oof())
Defining the macro as:
#define biff(pow) if (pow) kayo(); else oof();
causes difficulties with constructs like:
if (socko)
biff(flez);
else
bang(glurp);
Similar problems exist for the other permutations of defining biff().
Now for the RIGHT (!) definition of when a void expressions is valid:
A void expression is valid only under circumstances where the
value of an expression is not used.
This means that:
(a ? voidexp : voidexp),(a=b)
is valid. But a=a?voidexp:voidexp isn't valid, as the value is used. Apply
the rule above to all the cases and you have the RIGHT (!) answer. Note that
by implication, if the value of the ?: expression is not used, the operands
of the : need not be of compatible types, and need not be brought to a
common type.
I would like to see the ANSI C spec clarified on this point.
More information about the Comp.lang.c
mailing list