cond. op. on ='s LHS
Wayne Throop
throopw at sheol.UUCP
Mon Feb 18 13:51:51 AEST 1991
> rh at smds.UUCP (Richard Harter)
[.. discussing assignment to expressions like *(a==b?&c:&d) ..]
> First of all the trick of using
> *(cond?&var1:&var2) is not technically legal since the result of the
> ?: is not an lvalue. [Harbison & Steele]. I rather suspect you could
> sneak it past most compilers though.
Well... I'd say "all correct C compilers" really ought to be "most",
wouldn't you?
As Dan Bernstein has pointed out in another article, Richard is
incorrect here. While it is true that the result of ?: is never
an lvalue, this is irrelevant since the result of * always is.
> Secondly the syntax of the condition is, in its own right, ugly.
No uglier than much else of C's alphabet soup operators, I'd say.
> Thirdly, the use of the conditional in an lvalue obscures the destination
> of an assignment.
I agree, but this is appropriate if the destination is, in fact,
obscure or arcane. That is, I agree it is something to be avoided,
but I think it may sometimes be the least of the available evils.
> Fourthly, the ?: is hard to read (see second complaint above).
Again, I agree with the same reservation as in "thirdly" above.
> Fifthly, the original example is an instance of making code less efficient
> in the process of reducing the number of statements since you have to
> reference and dereference.
It's a pretty poor compiler that would be confused by this into
doing something noticeably inefficent.
On the other hand, I think it is Morally Wrong to invoke extraneous
operations. (And even if I'm a little vague on what can be classed
as extraneous, I would be perverse not to include the address-of
operation in the example under discussion as extraneous.)
Keep in mind: I agree that ?: is not well suited for the LHS
of expressions... I was just overreacting to what I perceived
as an oversimple blanket condemnation. (Before chastizing me
on grounds that this is all in my mind, realize that I KNOW that...
but taking what has been said as a blanket condemnation is an
error worth warning folks against, isn't it? Well... isn't it?)
--
Wayne Throop <backbone>!mcnc!rti!sheol!throopw or sheol!throopw at rti.rti.org
More information about the Comp.lang.c
mailing list