diff --git a/include/nlohmann/detail/meta/type_traits.hpp b/include/nlohmann/detail/meta/type_traits.hpp index 9851de430..8747a85e3 100644 --- a/include/nlohmann/detail/meta/type_traits.hpp +++ b/include/nlohmann/detail/meta/type_traits.hpp @@ -684,10 +684,11 @@ inline constexpr bool value_in_range_of(T val) template using bool_constant = std::integral_constant; -#ifdef JSON_HAS_CPP_20 - template - concept CompatibleType = !is_basic_json>::value && is_compatible_type>::value; -#endif +template > +struct json_compatible_type +{ + static constexpr auto 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 84d41ab98..2e116f104 100644 --- a/include/nlohmann/json.hpp +++ b/include/nlohmann/json.hpp @@ -3694,12 +3694,19 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec /// @brief comparison: equal /// @sa https://json.nlohmann.me/api/basic_json/operator_eq/ - template T> + 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); + } + /// @brief comparison: not equal /// @sa https://json.nlohmann.me/api/basic_json/operator_ne/ bool operator!=(const_reference rhs) const noexcept @@ -3726,9 +3733,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 71218c6c1..8ef520dc0 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -4091,10 +4091,11 @@ inline constexpr bool value_in_range_of(T val) template using bool_constant = std::integral_constant; -#ifdef JSON_HAS_CPP_20 - template - concept CompatibleType = !is_basic_json>::value && is_compatible_type>::value; -#endif +template > +struct json_compatible_type +{ + static constexpr auto value = !is_basic_json::value && is_compatible_type::value; +}; /////////////////////////////////////////////////////////////////////////////// // is_c_string @@ -22913,12 +22914,19 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec /// @brief comparison: equal /// @sa https://json.nlohmann.me/api/basic_json/operator_eq/ - template T> + 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); + } + /// @brief comparison: not equal /// @sa https://json.nlohmann.me/api/basic_json/operator_ne/ bool operator!=(const_reference rhs) const noexcept @@ -22945,9 +22953,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* }