Checked.opUnary

Defines unary operators +, -, ~, ++, and --. Unary + is not overridable and always has built-in behavior (returns this). For the others, if Hook defines hookOpUnary, opUnary forwards to Checked!(typeof(hook.hookOpUnary!op(get)), Hook)(hook.hookOpUnary!op(get)).

If Hook does not define hookOpUnary but defines onOverflow, opUnary forwards to hook.onOverflow!op(get) in case an overflow occurs. For ++ and --, the payload is assigned from the result of the call to onOverflow.

Note that unary - is considered to overflow if T is a signed integral of 32 or 64 bits and is equal to the most negative value. This is because that value has no positive negation.

  1. auto opUnary()
    struct Checked(T, Hook = Abort)
    opUnary
    (
    string op
    this _
    )
    ()
    if (
    op == "+" ||
    op == "-"
    ||
    op == "~"
    )
    if (
    is(T == Checked!(U, H),
    U
    H
    )
    )
  2. Checked opUnary()

Parameters

op

The unary operator

Return Value

Type: auto

A Checked instance representing the result of the unary operation

Examples

static struct MyHook
{
    static bool thereWereErrors;
    static L hookOpUnary(string x, L)(L lhs)
    {
        if (x == "-" && lhs == -lhs) thereWereErrors = true;
        return -lhs;
    }
}
auto a = checked!MyHook(long.min);
assert(a == -a);
assert(MyHook.thereWereErrors);
auto b = checked!void(42);
assert(++b == 43);

Meta