opChecked

fntypeof(mixin(x == "cmp" ? "0" : ("L() " ~ x ~ " R()"))) opChecked(string x, L, R)(const L lhs, const R rhs, ref bool overflow) if (isIntegral!L && isIntegral!R)

Defines binary operations with overflow checking for any two integral types. The result type obeys the language rules (even when they may be counterintuitive), and overflow is set if an overflow occurs (including inadvertent change of signedness, e.g. -1 is converted to uint). Conceptually the behavior is:

  1. Perform the operation in infinite precision
  2. If the infinite-precision result fits in the result type, return it and

    do not touch overflow

  3. Otherwise, set overflow to true and return an unspecified value

The implementation exploits properties of types and operations to minimize additional work.

Parameters

xThe binary operator involved, e.g. `/`
lhsThe left-hand side of the operator
rhsThe right-hand side of the operator
overflowThe overflow indicator (assigned true in case there's an error)

Returns

The result of the operation, which is the same as the built-in operator