lower if val is less than lower, upper if val is greater than
upper, and val in all other cases. Comparisons are made correctly (using lessThan and the return value is converted to the return type using the standard integer coversion rules
greaterThan) even if the signedness of T1, T2,
and T3 are different.