Flash Gordon said:
I don't consider the warning to be defective.
Note that "defective warnings" was intended by websnarf as
ironic. The original phrase was "errant warning message".
Being able to get a warning for loop control expressions
that are constant is perfectly ok; it is the message for
the particular case of 'while(1)' that is errant. The
followup by Keith Thompson articulates this nicely.
You can have conditions
which always evaluate to true or false for a number of reasons:
1) It will evaluate to a different value on some other implementation
leading to different behaviour and this is correct.
Why not use #if to select the appropriate behaviour so that it is
obvious that it never changes for a given build?
First, this approach applies to if's rather than while/for,
so it's not really germane to the topic under discussion,
which is warnings for 'while' control expressions that
are constant.
Second, using '#if' when 'if' can be used instead seems like
a step backwards. One nice thing about writing code with
'if' rather than '#if' is that the code in the conditional
always gets compiled (and so must compile correctly). Also,
consider something like:
if( DEFINED_1_OR_0 ) ...
else if( x > y ) ...
else ...
This code fragment can be rewritten using #if, but the form
of the result suffers.
As a development practice I always recommend using #if only
for sections of code that simply won't compile on other
platforms. There are of course some cases where it's better
to make an exception, but by and large code that uses 'if'
rather than '#if' tends to be more reliable and easier to
work on.
2) You are doing an "infinite" loop (it might break out from the middle)
and the for (;
idiom can be used in this instance.
This statement presumes that there is never any reason to
prefer 'while(1)' to 'for(;
', so the reasoning here is
rather circular. Contrary to the presumption, several
different reasons have been given for preferring the
'while(1)' form.
3) You've made an error (maybe due to mixing signed and unsigned or
something) and the condition should *not* always evaluate to the same
value.
Sure, but that doesn't have to do with looping necessarily.
The -W flag in gcc will generate warnings for expressions
like 'x >= 0' if x is unsigned. The concern is that the
expression is unexpectedly constant. The '1' in 'while(1)'
is constant, but it is not *unexpectely* constant.
[various comments that Flash prefers using 'for'
for infinite looping, and getting warnings for
any constant "conditional expression"]
For me while(1) is not more intuitive than for (;
. I'm not disputing
it is for you, just pointing out that for some of us there is not the
down side you have.
Yes, I understood that. I'm not advocating that anyone stop
using 'for(;
'. However, I think it's a mistake to make a
choice of this kind just because there's some warning
message that flags one form or the other. You wouldn't stop
using 'for(;
' just because some compiler decided to issue
a warning message for it, would you? Yet from my point of
view such a warning message has a (small) positive value (as
long as it can be turned off).
I definitely agree here. Both while(1) and for(;
are perfectly clear
if you no C, and I can see no instance where you would want to change
all instances of FOREVER to something other than an infinite loop, so it
adds something non-standard to be learned (you have to learn to always
use it) with no significant benefit that I can see.
Note that I wasn't advocating that 'FOREVER' be used; only
that I wouldn't have a problem with it if the team decided
that it should be used.
As for benefits, there are at least three potential benefits:
1. It stands out. It's nice to be able to pick out easily
the places infinite loops appear, especially if you
believe that infinite loops usually should be recast
so that they are not infinite. (The "infinite" here
really means "apparently infinite"; most "infinite"
loops have break's or return's that stop them from
being actual infinite loops.)
2. There can be benefit to being able to change what
happens during infinite looping, as noted in other
messages in this tree. You should be able to find
these if you use google groups and search for
'LOOPTRACK'.
3. If there is team consensus that 'FOREVER' should be
used, that itself is a benefit; having a team be
more unified provides positive value.
Whether these benefits are worth the costs naturally
depends on what the costs are judged to be.