Re: GCC 3.4 Heads-up

From: Linus Torvalds
Date: Sun Jan 04 2004 - 15:42:41 EST




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.

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.

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;
}

void test_assignment_2(int val)
{
if (val)
a = 1;
else
b = 1;
}

void test_assignment_3(int val)
{
(val ? a : b) = 1;
}

actually generate.

Hint: only the first one generates code without any jumps (on
architectures that have conditional moves and with "normal" compilers).

In short: the "*(a?&b:&c) = x" format actually has some advantages. It
also happens to be standard C. The "(a ? b : c) = x" format is not only
not real C code, but it has _zero_ advantages.

Linus
-
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/