Saturate.onUpperBound

Implements saturation for operators +=, -=, *=, /=, %=, ^^=, &=, |=, ^=, <<=, >>=, and >>>=. This hook is called if the result of the binary operation does not fit in Lhs without loss of information or a change in sign.

  1. T onLowerBound(Rhs , T bound)
  2. T onUpperBound(Rhs , T bound)
    struct Saturate
    static
    T
    onUpperBound
    (
    Rhs
    T
    )
    (
    Rhs
    ,)

Parameters

Rhs

The right-hand side type in the assignment, after the operation has been computed

bound T

The bound being violated

Return Value

Type: T

Lhs.max if rhs >= 0, Lhs.min otherwise.

Examples

auto x = checked!Saturate(short(100));
x += 33000;
assert(x == short.max);
x -= 70000;
assert(x == short.min);

Meta