smartOp.unary

Perform the unary (single-argument) integer operation specified by op.

Key differences from the standard unary operators:

  • - and + always return a signed value.
  • - is checked for overflow, because -int.min is greater than int.max.
  • ++ and -- are checked for overflow.


Note that like the standard operators, ++ and -- take the operand by ref and overwrite its value with the result.

  1. N unary(N num)
  2. IntFromChar!N unary(N num)
  3. Signed!(Promoted!N) unary(N num)
    template smartOp(IntFlagPolicy policy)
    @safe
    Signed!(Promoted!N)
    unary
    (
    string op
    N
    )
    (
    const N num
    )
    if (
    op.among!("-", "+")
    )
  4. N unary(N num)

Examples

import checkedint.noex : smartOp; // use IntFlagPolicy.noex

assert(smartOp.unary!"~"(0u) == uint.max);

auto a = smartOp.unary!"-"(20uL);
static assert(is(typeof(a) == long));
assert(a == -20);

auto b = smartOp.unary!"+"(uint.max);
static assert(is(typeof(b) == int));
assert(IntFlags.local.clear() == IntFlag.posOver);

uint c = 1u;
assert(smartOp.unary!"--"(c) == 0u);
assert(c == 0u);
smartOp.unary!"--"(c);
assert(IntFlags.local.clear() == IntFlag.negOver);

int d = 7;
assert(smartOp.unary!"++"(d) == 8);
assert(d == 8);

Meta