Skip to content

Instantly share code, notes, and snippets.

@vinniefalco
Created December 3, 2019 16:53
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/b6c7ed80539566daefec05ff3b470e6e to your computer and use it in GitHub Desktop.
Save vinniefalco/b6c7ed80539566daefec05ff3b470e6e to your computer and use it in GitHub Desktop.
template<class T>
T
number_cast(value const& jv, error_code& ec) noexcept
{
T result{};
if(! jv.is_number())
{
ec = error::not_number;
return result;
}
switch(jv.kind())
{
case kind::double_:
result = static_cast<T>(
jv.get_double());
if(result != jv.get_double())
{
ec = error::not_exact;
return result;
}
break;
case kind::int64:
result = static_cast<T>(
jv.get_int64());
if(result != jv.get_int64())
{
ec = error::not_exact;
return result;
}
break;
case kind::uint64:
result = static_cast<T>(
jv.get_uint64());
if(result != jv.get_uint64())
{
ec = error::not_exact;
return result;
}
break;
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment