Unable to find a logical reason behind why a <= operator gives a warning when the < and == operators do not. I have inclduded a test program which produces the warning when the program is compiled. Reproducible: Always Steps to Reproduce: 1.Make app: g++ -W -Wall -o test test.cpp (warning will be visible) Actual Results: g++ -o test test.cpp test.cpp: In function `int main(int, char*)': test.cpp:25: warning: comparison is always true due to limited range of data type Expected Results: g++ -o test test.cpp (No warning) #include <inttypes.h> #include <iostream> static const uint16_t LO_MAC = 0xFF00; static const uint16_t HI_MAC = 0xFFFF; int main (int, char*) { uint16_t b = 0xFFFF; if (b >= LO_MAC) { std::cout << "[OK] Low MAC: " << b << std::endl; } b = 0; if (b < HI_MAC) { std::cout << "[OK] Less than HI_MAC: " << b << std::endl; } b = HI_MAC; if (b == HI_MAC) { std::cout << "[OK] Equal to HI_MAC: " << b << std::endl; } b = 50; if (b <= HI_MAC) { std::cout << "[COMPILE WARNING] High MAC: " << b << std::endl; } return 0; }
Since a uint16_t cannot have a value greater than 0xFFFF the last check is correct. The problem was more programmer misunderstanding of type limits.