Skip to content

Commit

Permalink
use uncvref_t<T> instead of remove_cv_t<remove_reference_t<T>>>
Browse files Browse the repository at this point in the history
  • Loading branch information
Théo DELRIEU committed Oct 20, 2016
1 parent db4806b commit 9d290c6
Showing 1 changed file with 18 additions and 15 deletions.
33 changes: 18 additions & 15 deletions src/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ using remove_cv_t = typename std::remove_cv<T>::type;
template <typename T>
using remove_reference_t = typename std::remove_reference<T>::type;

template <typename T>
using uncvref_t = remove_cv_t<remove_reference_t<T>>;

// TODO update this doc
/*!
@brief unnamed namespace with internal helper functions
Expand Down Expand Up @@ -1315,11 +1318,12 @@ class basic_json
// auto j = json{{"a", json(not_equality_comparable{})}};
//
// we can remove this constraint though, since lots of ctor are not explicit already
template <typename T, typename = enable_if_t<detail::has_json_traits<
remove_cv_t<remove_reference_t<T>>>::value>>
template <typename T, typename = enable_if_t<
detail::has_json_traits<uncvref_t<T>>::value>>
explicit basic_json(T &&val)
: basic_json(json_traits<remove_cv_t<remove_reference_t<T>>>::to_json(
std::forward<T>(val))) {}
: basic_json(json_traits<uncvref_t<T>>::to_json(std::forward<T>(val)))
{
}
/*!
@brief create a string (explicit)
Expand Down Expand Up @@ -2681,25 +2685,24 @@ class basic_json

// get_impl overload chosen if json_traits struct is specialized for type T
// simply returns json_traits<T>::from_json(*this);
template <typename T, typename = enable_if_t<detail::has_json_traits<
remove_cv_t<remove_reference_t<T>>>::value>>
auto get_impl(T *) const
-> decltype(json_traits<remove_cv_t<remove_reference_t<T>>>::from_json(
std::declval<basic_json>())) {
return json_traits<remove_cv_t<remove_reference_t<T>>>::from_json(*this);
template <typename T, typename = enable_if_t<
detail::has_json_traits<uncvref_t<T>>::value>>
auto get_impl(T *) const -> decltype(
json_traits<uncvref_t<T>>::from_json(std::declval<basic_json>()))
{
return json_traits<uncvref_t<T>>::from_json(*this);
}

// this one is quite atrocious
// this overload is chosen ONLY if json_traits struct is not specialized, and if the expression nlohmann::from_json(*this, T&) is valid
// I chose to prefer the json_traits specialization if it exists, since it's a more advanced use.
// But we can of course change this behaviour
template <typename T>
auto get_impl(T *) const
-> enable_if_t<not detail::has_json_traits<remove_cv_t<T>>::value,
remove_cv_t<remove_reference_t<decltype(
::nlohmann::from_json(std::declval<basic_json>(),
auto get_impl(T *) const -> enable_if_t<
not detail::has_json_traits<remove_cv_t<T>>::value,
uncvref_t<decltype(::nlohmann::from_json(std::declval<basic_json>(),
std::declval<T &>()),
std::declval<T>())>>>
std::declval<T>())>>
{
remove_cv_t<T> ret;
// I guess this output parameter is the only way to get ADL
Expand Down

0 comments on commit 9d290c6

Please sign in to comment.