Skip to content

Commit

Permalink
use static_casts to silence MSVC warnings
Browse files Browse the repository at this point in the history
Those warnings are not "fixed", but that's intentional.
Truncations are expected when ArithmeticType is not the same type than
number_unsigned_t (or another basic_json template argument)
  • Loading branch information
theodelrieu committed Jan 12, 2017
1 parent c707efc commit 2f85d37
Showing 1 changed file with 32 additions and 33 deletions.
65 changes: 32 additions & 33 deletions src/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -486,11 +486,11 @@ void get_arithmetic_value(Json const &j, ArithmeticType &val)
{
// unsigned must be checked first, since is_number_integer() == true for unsigned
if (j.is_number_unsigned())
val = *j.template get_ptr<const typename Json::number_unsigned_t*>();
val = static_cast<ArithmeticType>(*j.template get_ptr<const typename Json::number_unsigned_t*>());
else if (j.is_number_integer())
val = *j.template get_ptr<const typename Json::number_integer_t*>();
val = static_cast<ArithmeticType>(*j.template get_ptr<const typename Json::number_integer_t*>());
else if (j.is_number_float())
val = *j.template get_ptr<const typename Json::number_float_t*>();
val = static_cast<ArithmeticType>(*j.template get_ptr<const typename Json::number_float_t*>());
else
throw std::domain_error("type must be number, but is " + type_name(j));
}
Expand All @@ -514,7 +514,7 @@ template <typename Json, typename FloatType,
enable_if_t<std::is_floating_point<FloatType>::value, int> = 0>
void to_json(Json &j, FloatType val) noexcept
{
external_constructor<value_t::number_float>::construct(j, val);
external_constructor<value_t::number_float>::construct(j, static_cast<typename Json::number_float_t>(val));
}


Expand All @@ -525,7 +525,7 @@ template <
int> = 0>
void to_json(Json &j, CompatibleNumberUnsignedType val) noexcept
{
external_constructor<value_t::number_unsigned>::construct(j, val);
external_constructor<value_t::number_unsigned>::construct(j, static_cast<typename Json::number_unsigned_t>(val));
}

template <
Expand All @@ -535,7 +535,7 @@ template <
int> = 0>
void to_json(Json &j, CompatibleNumberIntegerType val) noexcept
{
external_constructor<value_t::number_integer>::construct(j, val);
external_constructor<value_t::number_integer>::construct(j, static_cast<typename Json::number_integer_t>(val));
}

template <typename Json, typename UnscopedEnumType,
Expand Down Expand Up @@ -695,13 +695,13 @@ template <
void from_json(Json const &j, ArithmeticType &val)
{
if (j.is_number_unsigned())
val = *j.template get_ptr<const typename Json::number_unsigned_t*>();
val = static_cast<ArithmeticType>(*j.template get_ptr<const typename Json::number_unsigned_t*>());
else if (j.is_number_integer())
val = *j.template get_ptr<const typename Json::number_integer_t*>();
val = static_cast<ArithmeticType>(*j.template get_ptr<const typename Json::number_integer_t*>());
else if (j.is_number_float())
val = *j.template get_ptr<const typename Json::number_float_t*>();
val = static_cast<ArithmeticType>(*j.template get_ptr<const typename Json::number_float_t*>());
else if (j.is_boolean())
val = *j.template get_ptr<const typename Json::boolean_t*>();
val = static_cast<ArithmeticType>(*j.template get_ptr<const typename Json::boolean_t*>());
else
throw std::domain_error("type must be number, but is " + type_name(j));
}
Expand Down Expand Up @@ -732,20 +732,19 @@ constexpr bool has_adl_to_json(long)

struct to_json_fn
{
private:
template <typename Json, typename T, enable_if_t<has_adl_to_json<Json, T>(0), int> = 0>
auto operator()(Json&& j, T&& val) const
noexcept(noexcept(to_json(std::forward<Json>(j), std::forward<T>(val))))
-> decltype(to_json(std::forward<Json>(j), std::forward<T>(val)),
template <typename Json, typename T>
auto call(int, Json& j, T&& val) const
noexcept(noexcept(to_json(j, std::forward<T>(val))))
-> decltype(to_json(j, std::forward<T>(val)),
void())
{
return to_json(std::forward<Json>(j), std::forward<T>(val));
return to_json(j, std::forward<T>(val));
}

template <typename Json, typename T, enable_if_t<not has_adl_to_json<Json, T>(0), int> = 0>
void operator()(Json&&, T&&) const noexcept
template <typename Json, typename T>
void call(long, Json&, T&&) const noexcept
{
static_assert(has_adl_to_json<Json, T>(0), "to_json method in T's namespace can not be called");
static_assert(sizeof(Json) == 0, "to_json method in T's namespace can not be called");
}

public:
Expand All @@ -759,20 +758,20 @@ struct to_json_fn

struct from_json_fn
{
private:
template <typename Json, typename T, enable_if_t<has_adl_from_json<Json, T>(0), int> = 0>
auto operator()(Json&& j, T& val) const
noexcept(noexcept(from_json(std::forward<Json>(j), val)))
-> decltype(from_json(std::forward<Json>(j), val), void())
{
return from_json(std::forward<Json>(j), val);
}
template <typename Json, typename T, enable_if_t<not has_adl_from_json<Json, T>(0), int> = 0>
void operator()(Json&&, T&) const noexcept
{
static_assert(has_adl_from_json<Json, T>(0), "from_json method in T's namespace can not be called");
}
private:
template <typename Json, typename T>
auto call(int, Json const &j, T &val) const
noexcept(noexcept(from_json(j, val)))
-> decltype(from_json(j, val), void())
{
return from_json(j, val);
}

template <typename Json, typename T>
void call(long, Json const&, T&) const noexcept
{
static_assert(sizeof(Json) == 0, "from_json method in T's namespace can not be called");
}

public:
template <typename Json, typename T>
Expand Down

0 comments on commit 2f85d37

Please sign in to comment.