Shouldn't the following code produce a warning?

ric maicle via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Apr 3 22:23:16 PDT 2016


The values of the variables here can be determined during compilation.
Should there be some kind of warning like, unsigned integer may not be 
compared to a value less than zero; or there shouldn't be since the 
programmer should have known what he's doing?

import std.stdio;

void f1() {
     uint n = 0;
     uint ans = n - 1u;
     if (ans < 0) {				// warning?
         writeln("Less than or equal 0.");
     } else {
         writeln("Just like C++.");
     }
}

void f2() {
     const uint n = 0;
     const uint ans = n - 1u;
     static if (ans < 0) {			// warning?
         writeln("Less than or equal 0.");
     } else {
         writeln("Just like C++.");
     }
}

void main() {
     f1();
     f2();
}


More information about the Digitalmars-d-learn mailing list