View source code
Display the source code in std/checkedint.d from which this page was generated on github.
Report a bug
If you spot a problem with this page, click here to create a Bugzilla issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using local clone.

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.onBadCast!U(get) 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.onBadCast!U(get) is returned.

Parameters

NameDescription
U The type to cast to

Returns

If Hook defines hookOpCast, the call immediately returns hook.hookOpCast!U(get). 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

Authors

License