Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change operator== overload that constrains to is_scalar_v to constrain to compatible types. #4181

Open
wants to merge 14 commits into
base: develop
Choose a base branch
from
Open
7 changes: 7 additions & 0 deletions include/nlohmann/detail/meta/type_traits.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -684,6 +684,13 @@ inline constexpr bool value_in_range_of(T val)
template<bool Value>
using bool_constant = std::integral_constant<bool, Value>;


template <typename T, typename BasicJsonType, typename U = uncvref_t<T>>
struct json_compatible_type
{
static constexpr bool value = !is_basic_json<U>::value && is_compatible_type<BasicJsonType, U>::value;
};

///////////////////////////////////////////////////////////////////////////////
// is_c_string
///////////////////////////////////////////////////////////////////////////////
Expand Down
27 changes: 21 additions & 6 deletions include/nlohmann/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3694,9 +3694,24 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec

/// @brief comparison: equal
/// @sa https://json.nlohmann.me/api/basic_json/operator_eq/
template<typename ScalarType>
requires std::is_scalar_v<ScalarType>
bool operator==(ScalarType rhs) const noexcept
template <typename T>
requires detail::json_compatible_type<T, basic_json_t>::value
bool operator==(T rhs) const noexcept
{
return *this == basic_json(rhs);
}

/// @brief comparison: not equal
/// @sa https://json.nlohmann.me/api/basic_json/operator_eq/
template <typename T>
requires detail::json_compatible_type<T, basic_json_t>::value
bool operator!=(T rhs) const noexcept
{
return *this != basic_json(rhs);
}

/// @sa https://json.nlohmann.me/api/basic_json/operator_eq/
bool operator==(std::nullptr_t rhs) const noexcept
{
return *this == basic_json(rhs);
}
Expand Down Expand Up @@ -3727,9 +3742,9 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec

/// @brief comparison: 3-way
/// @sa https://json.nlohmann.me/api/basic_json/operator_spaceship/
template<typename ScalarType>
requires std::is_scalar_v<ScalarType>
std::partial_ordering operator<=>(ScalarType rhs) const noexcept // *NOPAD*
template <typename T>
requires detail::json_compatible_type<T, basic_json_t>::value
std::partial_ordering operator<=>(T rhs) const noexcept // *NOPAD*
{
return *this <=> basic_json(rhs); // *NOPAD*
}
Expand Down
34 changes: 28 additions & 6 deletions single_include/nlohmann/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4091,6 +4091,13 @@ inline constexpr bool value_in_range_of(T val)
template<bool Value>
using bool_constant = std::integral_constant<bool, Value>;


template <typename T, typename BasicJsonType, typename U = uncvref_t<T>>
struct json_compatible_type
{
static constexpr bool value = !is_basic_json<U>::value && is_compatible_type<BasicJsonType, U>::value;
};

///////////////////////////////////////////////////////////////////////////////
// is_c_string
///////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -22908,9 +22915,24 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec

/// @brief comparison: equal
/// @sa https://json.nlohmann.me/api/basic_json/operator_eq/
template<typename ScalarType>
requires std::is_scalar_v<ScalarType>
bool operator==(ScalarType rhs) const noexcept
template <typename T>
requires detail::json_compatible_type<T, basic_json_t>::value
bool operator==(T rhs) const noexcept
{
return *this == basic_json(rhs);
}

/// @brief comparison: not equal
/// @sa https://json.nlohmann.me/api/basic_json/operator_eq/
template <typename T>
requires detail::json_compatible_type<T, basic_json_t>::value
bool operator!=(T rhs) const noexcept
{
return *this != basic_json(rhs);
}

/// @sa https://json.nlohmann.me/api/basic_json/operator_eq/
bool operator==(std::nullptr_t rhs) const noexcept
{
return *this == basic_json(rhs);
}
Expand Down Expand Up @@ -22941,9 +22963,9 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec

/// @brief comparison: 3-way
/// @sa https://json.nlohmann.me/api/basic_json/operator_spaceship/
template<typename ScalarType>
requires std::is_scalar_v<ScalarType>
std::partial_ordering operator<=>(ScalarType rhs) const noexcept // *NOPAD*
template <typename T>
requires detail::json_compatible_type<T, basic_json_t>::value
std::partial_ordering operator<=>(T rhs) const noexcept // *NOPAD*
{
return *this <=> basic_json(rhs); // *NOPAD*
}
Expand Down
1 change: 1 addition & 0 deletions tests/src/unit-comparison.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,7 @@ TEST_CASE("lexicographical comparison operators")
CAPTURE(i)
CAPTURE(j)
CHECK((j_values[i] == j_values[j]) == expected_eq[i][j]);
CHECK(expected_eq[i][j] == (j_values[i] == j_values[j]));
}
}

Expand Down