Assuming there is a declaration in a header file you don't control that states something like:
static const uint16 MaxValue = 0xffff; // the type could be anything, static or not
Inside a file that includes the above you have code like this:
int some_function(uint16 n) {
if (n > MaxValue) {
n = MaxValue;
}
do_something(n);
...
}
The compiler will warn that the if statement is always false because n
cannot be larger than 0xffff.
One way might be to remove the code. But then if later someone wants to change the value of MaxValue
to something lower you have just introduced a bug.
Two questions:
MaxValue
is 0xfff) and included (when MaxValue
is not 0xffff)?MaxValue
is equal to the limit the type can hold, is there a portable technique to use to identify the maximum value for the type of MaxValue
that would work if later the code of MaxValue
is changed?
limits.h
or <limit>
constants like USHRT_MAX
, or event not using std::numeric_limits<uint16>
explicitly.std:numeric_limits<MaxValue>
be used?typeid results in a type_info const & rather than the type of variable, use
std::numeric_limits<decltype(variable)>::max()
instead.