Suppose I have a function like:
#define LOWER_BOUND 0 #define UPPER_BOUND 42 int is_value_in_range( some_typedef val) { return ((LOWER_BOUND <= val) && (val <= UPPER_BOUND)); }
Assuming that I have warnings configured accordingly, if some_typedef is unsigned, I will get a warning that there is a meaningless comparison of the unsigned type with 0. Of course, this is true, and it makes sense.
However, let's say that I want the check against zero to be in the code for one or several possible reasons, for example:
- While borders will always be compile-time constants, they may be something that can change (and macros may not "live" very close to the function). For example, boundaries can be specified by passing options to the compiler.
- I would like to protect myself from a later typedef change to a signed type, since it is possible that every use of typedef cannot be carefully checked when changing it.
Is there a decent, reasonably portable way to drown out a warning here without shutting it down completely?
Something that relies on the STATIC_ASSERT () functionality that is available to me would be acceptable if it were reasonable. I'm fine with a compilation violation if the type changes to make someone look at the code. But it may be important to note that typeof is not what I have in all the compilers I am aiming for.
I am specifically looking for C-language solutions, so templates are not needed here ...
c warnings
Michael burr
source share