Function std.checkedint.Checked.opCast
Casting operator to integral, bool
, or floating point type.
U opCast(U, _)()
if (isIntegral!U || isFloatingPoint!U || is(U == bool));
If a cast to a floating-point type is requested and Hook
defines
onBadCast
, the cast is verified by ensuring get == cast(T)
U(get)
. If that is not true
, hook
is returned.
If a cast to an integral type is requested and Hook
defines onBadCast
,
the cast is verified by ensuring get
and cast(U)
get
are the same arithmetic number. (Note that int(-1)
and
uint(1)
are different values arithmetically although they have the same
bitwise representation and compare equal by language rules.) If the numbers
are not arithmetically equal, hook
is
returned.
Parameters
Name | Description |
---|---|
U | The type to cast to |
Returns
If Hook
defines hookOpCast
, the call immediately returns
hook
. Otherwise, casting to bool
yields get != 0
and casting to another integral that can represent all
values of T
returns get
promoted to U
.
Example
writeln(cast(uint)checked(42)); // 42
writeln(cast(uint)checked!WithNaN(-42)); // uint.max