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 std.functional.lessThan and the return value is converted to the return type using the standard integer coversion rules std.functional.greaterThan) even if the signedness of T1, T2, and T3 are different.
assert(clamp(2, 1, 3) == 2); assert(clamp(0, 1, 3) == 1); assert(clamp(4, 1, 3) == 3); assert(clamp(1, 1, 1) == 1); assert(clamp(5, -1, 2u) == 2); auto x = clamp(42, uint.max, uint.max); static assert(is(typeof(x) == int)); assert(x == -1);
Clamps val into the given bounds. Result has the same type as val.