[comp.lang.c] A question about the tertiary

jfh@rpp386.Dallas.TX.US (John F. Haugh II) (08/22/89)

In article <612@cybaswan.UUCP> iiitsh@cybaswan.UUCP (Steve Hosgood) writes:
>I never noticed until last night that the conditional operator:
>
><condition>? <val1>: <val2>
>
>..is not a valid lvalue, even if both <val1> and <val2> are lvalues!
>I wanted to say:
>
>(flag > 0? foo: bar) = <complex expression>;
>
>..which didn't work, needless to say. I had to employ a temporary variable
>and an 'if' statement in the end :-(.

There is a way to do this ...

	*((type_of_foobar *) flag > 0 ? &foo:&bar) = <complex expression>
-- 
John F. Haugh II                        +-Quote of the month club: ------------
VoiceNet: (512) 832-8832   Data: -8835  |  Speaking of Netnews Administration:
InterNet: jfh@rpp386.cactus.org         |  "If Bill Vajk can do it, anyone can"
UUCPNet:  {texbell|bigtex}!rpp386!jfh   +---------     -- Patricia O Tuama-----

karzes@mfci.UUCP (Tom Karzes) (08/24/89)

In article <16928@rpp386.Dallas.TX.US> jfh@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.

flaps@dgp.toronto.edu (Alan J Rosenthal) (08/24/89)

>You should simply be able to write what you want, and let the
>compiler generate the best code it can for it.

Should we give this man a lollipop?

mccaugh@s.cs.uiuc.edu (08/25/89)

 I don't think I'm over-generalizing when I say that most of us would agree it
 would be nice to be able to write what we say (and not have to go to heroic
 lengths to get cooperation from the compiler), but the value of examples like
 this is that when someone finally does write a high-level optimizing C com-
 piler, they will have a wealth of ideas to choose from in order to implement
 just the user-friendly sort of translator we all want.