safeOp.unary

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

Trying to negate - an unsigned value will generate a compile-time error, because mathematically, the result should always be negative (except for -0), but the unsigned return type cannot represent this.

++ and -- are checked for overflow at runtime, and will raise IntFlag.posOver or IntFlag.negOver if needed.

  1. OpType!(op, N) unary(N num)
  2. N unary(N num)
    template safeOp(IntFlagPolicy policy)
    ref @safe
    N
    unary
    (
    string op
    N
    )
    (
    return ref N num
    )
    if (
    isIntegral!N &&
    op.among!("++", "--")
    )

Examples

import checkedint.sticky : safeOp; // use IntFlagPolicy.sticky

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

assert(safeOp.unary!"-"(20L) == -20L);
static assert(!__traits(compiles, safeOp.unary!"-"(20uL)));
safeOp.unary!"-"(long.min);
assert(IntFlags.local.clear() == IntFlag.posOver);

auto a = safeOp.unary!"+"(uint.max);
static assert(is(typeof(a) == uint));
assert(a == uint.max);

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

int c = 7;
assert(safeOp.unary!"++"(c) == 8);
assert(c == 8);

Meta