Skip to content

Instantly share code, notes, and snippets.

@vinniefalco
Created December 3, 2019 18:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vinniefalco/b5ede35e39e5e94f6fdb571ba237f8e9 to your computer and use it in GitHub Desktop.
Save vinniefalco/b5ede35e39e5e94f6fdb571ba237f8e9 to your computer and use it in GitHub Desktop.
template<class T>
T
number_cast(
value const& jv,
error_code& ec,
typename std::enable_if<
std::is_floating_point<T>::value
>::type* = 0
) noexcept
{
switch(jv.kind())
{
case kind::int64:
{
auto const i = jv.get_int64();
auto const t = static_cast<T>(i);
if(t != i)
ec = error::not_exact;
return t;
}
case kind::uint64:
{
auto const u = jv.get_uint64();
auto const t = static_cast<T>(u);
if(t != u)
ec = error::not_exact;
return t;
}
default:
case kind::double_:
{
auto const d = jv.get_double();
auto const t = static_cast<T>(d);
if(t != d)
ec = error::not_exact;
return t;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment