Re: [OFFTOPIC] Re: A bit off-topic ... (fwd)

J. S. Connell (ankh@canuck.gen.nz)
Wed, 31 Mar 1999 16:57:24 -0500 (EST)


On Wed, 31 Mar 1999, Rivalino Matias Junior wrote:

> Okay. Try in C++ compiler (e.g. g++). I'm using g++ in this case.

#include <stdio.h>

void main() {
int x = 0;
int y = 1;

x = (y) ? x = 1 : x = 2;
printf("%d\n", x);
}

gcc version: 2.7.2.3
egcc version: 2.91.61 19990216
g++ version: 2.91.61 19990216

20145 16:50:24 jsc@squeak:~% gcc -g -o test test.c
test.c: In function `main':
test.c:7: invalid lvalue in assignment
20146 16:50:28 jsc@squeak:~% egcc -g -o test test.c
test.c: In function `main':
test.c:7: invalid lvalue in assignment
20147 16:50:34 jsc@squeak:~% g++ -g -o test test.cc
20148 16:50:43 jsc@squeak:~% ./test
1

I believe that gcc/egcc are buggy here - your code _should_ be legal.
Also, whatever version of g++ you are using is _also_ buggy - it should be
generating code to produce 1, not 2, but it is not.

> >I suspect there's a compiler bug to do with assignments in a
> > ternary operator as the rvalue of an assignment.
>
> The expression is correct.

The _expression_ is correct - the _compiler_ is buggy.

> > Wrapping the two x='s on
> > the right-hand side generates the expected output.
>
> No. The expected output is X equal to the result of two other expression
> (E1,E2), related to condition (y).
> x= (condition) ? E1 : E2;

I understand how ?: works. What I said was that gcc/egcc would not compile
this line:
x = (y) ? x = 1 : x = 2;
But if you change it like this:
x = (y) ? (x = 1) : (x = 2);
then gcc/egcc don't give you an error. Try the same workaround in your own
compiler, or upgrade it.

--Jeff

-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.rutgers.edu
Please read the FAQ at http://www.tux.org/lkml/