[cfe-dev] How can I use the result of a void expression? :p

Chris Lattner clattner at apple.com
Mon Aug 27 20:38:17 PDT 2007


On Aug 27, 2007, at 1:51 PM, Keith Bauer wrote:

> My point is that in my mind, the warning is there to tell the
> programmer that they've probably done something stupid.

Yep.

> In the case of an expression without side effects, that's clearly  
> the case
> (whether the result is void or not).  Even in the situation where the
> expression is nonvoid, if it has side effects, it's probably still
> doing what I want.

Yes.  This is why we don't warn for things like:

int x;
++x;             // returns int
printf("foo");   // returns int

etc.  However, we do warn for useless computation like:

(x);
+x;
printf("foo")+printf("foo");

etc.

We don't warn for operators that have side effects.  ?: does not have  
a side effect, so we warn for it.

Similar to your use of ?:, we warn for:

foo() || bar();

because || does not have a side effect.  Note that C is not perl,  
just like C is not ML :).  If you want to use conditional code, using  
an if statement is a perfectly reasonable thing to do.

> Since the spec allows ?: to work on void, it's obviously intended that
> it can be used as syntactic shorthand for an if statement.  Why
> shouldn't it be usable for expressions which happen to be nonvoid, but
> where the side-effect is the more important part - printf/fprintf for
> example?

I'm not really sure what to say here.  What the spec says is not the  
issue.  Warnings, again, are for valid code - erroneous code is  
rejected by the compiler.  Except for a couple of strange things, the  
standard has nothing to say about warnings.

-Chris



More information about the cfe-dev mailing list