gcc wont compile code with ?: operator

Matthew Saltzman mjs at ces.clemson.edu
Mon Feb 16 22:25:38 UTC 2004


On Mon, 16 Feb 2004, Alexandre Oliva wrote:

> On Feb 16, 2004, "John V. Pope" <jpope_rhn at popes.net> wrote:
>
> > The following code is a simple demonstration of the bug and should compile without errors:
>
> As others have pointed out, the meaning this had with older versions
> of GCC is not what you expect:
>
> > 		isprint(a[x]) ? b[x] = a[x]: b[x] = '.';
>
> this meant:
>
>  		(isprint(a[x]) ? (b[x] = a[x]): b[x]) = '.';
>
> i.e., it would assign '.' to b[x] regardless of the result.
>
> That was because, if both branches of a ternary operator were lvalues,
> GCC would, as an extension, consider the result of the expression to
> be an lvalue as well.

Just a technicality:  "b[x] = a[x]" is not an lvalue.  I think the
extension construct was more like this:

		(flag == set_a) ? a : b = 1

which would set a to 1 if flag == set_a and would set b to 1 otherwise.

> Precisely because of coding errors like the one you mention, this
> extension was removed, so now you get an error, as you should always
> have.  And now you know the code is broken and has to be fixed.

Amen!

-- 
		Matthew Saltzman

Clemson University Math Sciences
mjs AT clemson DOT edu
http://www.math.clemson.edu/~mjs





More information about the fedora-list mailing list