Function std.experimental.checkedint.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
.
auto auto opUnary(string op, _)()
if (op == "+" || op == "-" || op == "~");
ref Checked opUnary(string op)() return
if (op == "++" || op == "--");
If Hook
does not define hookOpUnary
but defines onOverflow
, opUnary
forwards to hook
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.
Example
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);
writeln(a); // -a
assert(MyHook .thereWereErrors);
auto b = checked!void(42);
writeln(++b); // 43