Nothrow std.conv.to with explicit default value
Per Nordlöw
per.nordlow at gmail.com
Mon Jun 18 20:48:55 UTC 2018
I have a nothrow variant of std.conv.to defined as follows:
T toDefaulted(T, S, U)(S value, /*lazy*/ U defaultValue)
if (is(typeof(() { T r = defaultValue; }))) // TODO use
std.traits.isAssignable!(T, U) ?
{
try
{
import std.conv : to;
return value.to!T;
}
catch (Exception e) // assume `ConvException`. TODO can we
capture `ConvException` instead make it inferred `nothrow`
{
return defaultValue;
}
}
The problem with this code is that throwing exceptions for the
default case is costly at least with dmd. Is there another way to
do this?
More information about the Digitalmars-d-learn
mailing list