Christopher Benson-Manica wrote:
REH <sp******@stny.rr.com> wrote:
1 and 2 are signed constants. Try 1u and 2u.
That did silence the warning, although I find it strange that it would
warn on the code I posted and never on, say,
for( unsigned int foo=0; foo < 5; foo++ );
since foo < 5 should be the same signed/unsigned comparison that it
complains about on the code I posted.
Apparently you just found the limit of that compiler's intelligence when it
comes to analyzing situations like this. In this last example the signed value
is an integral-constant expression and its value is known at compile time. The
compiler can immediately see that the value is positive and that's it is safe to
perform the comparison in unsigned context (as the language requires).
In the original case the value of the expression depended on a non-constant
boolean value, which means that the compiler could not immediately predict the
actual signed value at compile time. Of course, if it could analyze it a bit
deeper, it'd see either that 'foo' is always 'false' or that the two possible
results '1' and '2' are both positive, which means that there was no problem in
that case as well. But this compiler simply doesn't do this type of deeper
analysis. It is, once again, a pure QoI issue.
If it interests you, you might also want to try this
const bool foo=false; // <- note the 'const'
for( unsigned int bar=0; bar < (foo?1:2); bar++ ) {
bar=42;
and see what happens in this case.
--
Best regards,
Andrey Tarasevich