[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: GCC 3.4 Heads-up




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 _at_ vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


この情報があなたの探していたものかどうか選択してください。
yes/まさにこれだ!   no/違うなぁ   part/一部見つかった   try/これで試してみる

あなたが探していた情報はどのようなことか、ご自由に記入下さい。特に「まさにこれだ!」と言う場合は記入をお願いします。
例:「複数のマシンからCATV経由でipmasqueradeを利用してWebを参照したい場合の設定について」
Follow-Ups: References: