Re: GCC 3.4 Heads-up

From: Bill Davidsen
Date: Sun Jan 04 2004 - 20:29:37 EST


Linus Torvalds wrote:

On Sun, 4 Jan 2004, Bill Davidsen wrote:

Since that's a matter of taste I can't disagree. The point was that the original post used
*(a ? &b : &c) = d;
which generates either warnings or errors if b or c is a register variable, because you are not allowed to take the address of a register.


The thing is, the above case is the _only_ case where there is any point
to using a conditional expression and an assignment to the result in the
same expression.

We disagree, see below.

If you have local variables (register or not), the sane thing to do is

if (a)
b = d;
else
c = d;

or variations on that. That's the readable code.

But may lead to errors in maintenence. Your first example below avoids that problem. Imagine instead of "d" you have a 40-50 character RHS. Now imagine that the code needs to be changed. If you have the long expression in two places then it invites the possiblility of someone changing only one of them. You may never make mistakes, but the rest of us do, and the conditional LHS avoids that.

The only case where conditional assignment expressions are useful is when
you are literally trying to avoid doing branches, and the compiler isn't
smart enough to avoid them otherwise.

Check the compiler output some day. Try out what

int a, b;

void test_assignment_1(int val)
{
*(val ? &a : &b) = 1;
}

You pointed out that this generates good code, I pointed out that it also avoids future errors and is not just a trick for broken compilers. I take your point about generating good code, I'm sorry you can't see that avoiding code duplication is good practice even without the benefit of better code.

--
bill davidsen <davidsen@xxxxxxx>
CTO TMR Associates, Inc
Doing interesting things with small computers since 1979
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/