gcc wont compile code with ?: operator

Alexandre Oliva aoliva at redhat.com
Mon Feb 16 18:44:08 UTC 2004


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.

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.

-- 
Alexandre Oliva   Enjoy Guarana', see http://www.ic.unicamp.br/~oliva/
Happy GNU Year!                     oliva@{lsd.ic.unicamp.br, gnu.org}
Red Hat GCC Developer                 aoliva@{redhat.com, gcc.gnu.org}
Free Software Evangelist                Professional serial bug killer





More information about the fedora-list mailing list