This commit is contained in:
Fredrik Sandhei 2025-02-22 06:10:00 +01:00 committed by GitHub
commit 36e2c2f255
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 57 additions and 12 deletions

View File

@ -742,6 +742,13 @@ 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
///////////////////////////////////////////////////////////////////////////////

View File

@ -3722,9 +3722,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);
}
@ -3755,9 +3770,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*
}

View File

@ -4277,6 +4277,13 @@ 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
///////////////////////////////////////////////////////////////////////////////
@ -23721,9 +23728,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);
}
@ -23754,9 +23776,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*
}

View File

@ -357,6 +357,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]));
}
}