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)
T
to
@safe
(
S
)
(
const S value
)
if (
useFlags!S
)

Examples

1 // Conversions involving only basic scalars or checkedint types use IntFlags for error signalling.
2 import checkedint.sticky : smartInt, SmartInt, smartOp, to; // use IntFlagPolicy.sticky
3 
4 assert(to!int(smartInt(-421751L)) == -421751);
5 assert(to!(SmartInt!ubyte)(100) == 100u);
6 
7 assert(is(typeof(to!int(50u)) == int));
8 assert(to!int(50u) == 50);
9 assert(!IntFlags.local);
10 
11 // If IntFlagPolicy.sticky is set, failed conversions return garbage, but...
12 assert(smartOp.cmp!"!="(to!int(uint.max), uint.max));
13 // ...IntFlags.local can be checked to see if anything went wrong.
14 assert(IntFlags.local.clear() == IntFlag.posOver);
1 // Everything else forwards to std.conv.to().
2 assert(to!(string, IntFlagPolicy.throws)(55) == "55");
3 assert(to!(real, IntFlagPolicy.throws)("3.141519e0") == 3.141519L);
4 
5 // Setting IntFlagPolicy.sticky or .asserts will block std.conv.to(), unless the instantiation is nothrow.
6 // Setting IntFlagPolicy.asserts or .throws will block std.conv.to(), unless the instantiation is pure.
7 static assert(!__traits(compiles, to!(real, IntFlagPolicy.sticky)("3.141519e0")));

Meta