to

A wrapper for std.conv.to() which uses checkedint.flags for error signaling when converting between any combination of basic scalar types and checkedint types. With an appropriate policy, this allows checkedint.to() to be used for numeric conversions in pure nothrow code, unlike std.conv.to().

Conversions involving any other type are simply forwarded to std.conv.to(), with no runtime overhead.

template to(T, IntFlagPolicy policy)
@safe
T
to
(
S
)
(
const S value
)
if (
useFlags!S
)

Members

Functions

to
T to(S value)
Undocumented in source. Be warned that the author may not have intended to support it.

Examples

// Conversions involving only basic scalars or checkedint types use IntFlags for error signalling.
import checkedint.noex : smartInt, SmartInt, smartOp, to; // use IntFlagPolicy.noex

assert(to!int(smartInt(-421751L)) == -421751);
assert(to!(SmartInt!ubyte)(100) == 100u);

assert(is(typeof(to!int(50u)) == int));
assert(to!int(50u) == 50);
assert(!IntFlags.local);

// If IntFlagPolicy.noex is set, failed conversions return garbage, but...
assert(smartOp.cmp!"!="(to!int(uint.max), uint.max));
// ...IntFlags.local can be checked to see if anything went wrong.
assert(IntFlags.local.clear() == IntFlag.posOver);
// Everything else forwards to std.conv.to().
assert(to!(string, IntFlagPolicy.throws)(55) == "55");
assert(to!(real, IntFlagPolicy.throws)("3.141519e0") == 3.141519L);

// Setting IntFlagPolicy.noex or .asserts will block std.conv.to(), unless the instantiation is nothrow.
// Setting IntFlagPolicy.asserts or .throws will block std.conv.to(), unless the instantiation is pure.
static assert(!__traits(compiles, to!(real, IntFlagPolicy.noex)("3.141519e0")));

Meta