A question about the tertiary (? : ) operator

Tom Karzes karzes at mfci.UUCP
Thu Aug 24 07:19:46 AEST 1989


In article <16928 at rpp386.Dallas.TX.US> jfh at rpp386.cactus.org (John F. Haugh II) writes:
>There is a way to do this ...
>
>	*((type_of_foobar *) flag > 0 ? &foo:&bar) = <complex expression>

Of course there are ways to do it, the point is that the DESIRED way doesn't
work.  Taking the address and doing an indirect store is gross.  Furthermore,
what if one of foo or bar is a register variable?  Using an if/else is also
gross.  You should simply be able to write what you want, and let the
compiler generate the best code it can for it.  If the expression is
structure valued, then most compilers would probably want to use the above
sequence.  Otherwise, if the expression fits in a register, using a
branch might be faster, or perhaps even using specialized hardware, if
available.  Or perhaps using pointers really would be better in some cases.
Sure, maybe a compiler could recognize the above and eliminate the
indirection when it is undesirable, but it's stupid to have to go through
all of that rather than simply write what you wanted to say in the first
place.



More information about the Comp.lang.c mailing list