Is there any way to indicate to the compiler that you know the value of a particular variable must be within a particular range at a certain point in the code, to assist the compiler with optimizing? I'm writing a library that makes it possible to know the range of some variables at compile time, and it would be nifty if it could somehow communicate this information to the compiler so that the compiler could use it for optimization. I'd like to add support for any compilers where it would work even if it couldn't be made to work for all of them (it sounds like the sort of thing that some compilers could have as an extension, but I haven't found any). I know I could write something like this:
if(x < COMPILE_TIME_MIN or x > COMPILE_TIME_MAX)
return;
// compiler will assume for code below that x is in range COMPILE_TIME_MIN..COMPILE_TIME_MAX
But that's a runtime check. Maybe there's some trick to get the compiler to make an assumption about the range without such a check?