diff --git a/include/nlohmann/detail/meta/type_traits.hpp b/include/nlohmann/detail/meta/type_traits.hpp index 678fac569..74e4dacfb 100644 --- a/include/nlohmann/detail/meta/type_traits.hpp +++ b/include/nlohmann/detail/meta/type_traits.hpp @@ -742,6 +742,13 @@ constexpr bool value_in_range_of(T val) template using bool_constant = std::integral_constant; + +template > +struct json_compatible_type +{ + static constexpr bool value = !is_basic_json::value && is_compatible_type::value; +}; + /////////////////////////////////////////////////////////////////////////////// // is_c_string /////////////////////////////////////////////////////////////////////////////// diff --git a/include/nlohmann/json.hpp b/include/nlohmann/json.hpp index 7a857e655..089325fd9 100644 --- a/include/nlohmann/json.hpp +++ b/include/nlohmann/json.hpp @@ -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 - requires std::is_scalar_v - bool operator==(ScalarType rhs) const noexcept + template + requires detail::json_compatible_type::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 + requires detail::json_compatible_type::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 - requires std::is_scalar_v - std::partial_ordering operator<=>(ScalarType rhs) const noexcept // *NOPAD* + template + requires detail::json_compatible_type::value + std::partial_ordering operator<=>(T rhs) const noexcept // *NOPAD* { return *this <=> basic_json(rhs); // *NOPAD* } diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 07dea3c4a..7720fdd68 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -4277,6 +4277,13 @@ constexpr bool value_in_range_of(T val) template using bool_constant = std::integral_constant; + +template > +struct json_compatible_type +{ + static constexpr bool value = !is_basic_json::value && is_compatible_type::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 - requires std::is_scalar_v - bool operator==(ScalarType rhs) const noexcept + template + requires detail::json_compatible_type::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 + requires detail::json_compatible_type::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 - requires std::is_scalar_v - std::partial_ordering operator<=>(ScalarType rhs) const noexcept // *NOPAD* + template + requires detail::json_compatible_type::value + std::partial_ordering operator<=>(T rhs) const noexcept // *NOPAD* { return *this <=> basic_json(rhs); // *NOPAD* } diff --git a/tests/src/unit-comparison.cpp b/tests/src/unit-comparison.cpp index 7cd897307..1a270cd48 100644 --- a/tests/src/unit-comparison.cpp +++ b/tests/src/unit-comparison.cpp @@ -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])); } }