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(const N num)
    template smartOp(IntFlagPolicy policy)
    N
    unary
    pure @safe nothrow @nogc
    (
    string op
    N
    )
    (
    const N num
    )
    if (
    isIntegral!N &&
    op == "~"
    )
  2. IntFromChar!N unary(const N num)
  3. Signed!(Promoted!N) unary(const N num)
  4. N unary(return ref N num)

Examples

1 import checkedint.sticky : smartOp; // use IntFlagPolicy.sticky
2 
3 assert(smartOp.unary!"~"(0u) == uint.max);
4 
5 auto a = smartOp.unary!"-"(20uL);
6 static assert(is(typeof(a) == long));
7 assert(a == -20);
8 
9 auto b = smartOp.unary!"+"(uint.max);
10 static assert(is(typeof(b) == int));
11 assert(IntFlags.local.clear() == IntFlag.posOver);
12 
13 uint c = 1u;
14 assert(smartOp.unary!"--"(c) == 0u);
15 assert(c == 0u);
16 smartOp.unary!"--"(c);
17 assert(IntFlags.local.clear() == IntFlag.negOver);
18 
19 int d = 7;
20 assert(smartOp.unary!"++"(d) == 8);
21 assert(d == 8);

Meta