Skip to content

Commit

Permalink
Don't use concept. Causes compilation error on GCC 10
Browse files Browse the repository at this point in the history
Opting out of using concept and rather defining a structure while still
being able to not use SFINAE and instead `requires` expression.

Compiling with C++23 on GCC 13.2.1 fails on overload resolution on
calling operator== for `std::nullptr_t`. The solution for this was to
simply add another overload for nullptr, on par with the equivalent
`nullptr_t` constructor.
  • Loading branch information
fsandhei committed Oct 11, 2023
1 parent ee57287 commit f68d2b1
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 16 deletions.
9 changes: 5 additions & 4 deletions include/nlohmann/detail/meta/type_traits.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -684,10 +684,11 @@ inline constexpr bool value_in_range_of(T val)
template<bool Value>
using bool_constant = std::integral_constant<bool, Value>;

#ifdef JSON_HAS_CPP_20
template <typename T, typename BasicJsonType>
concept CompatibleType = !is_basic_json<uncvref_t<T>>::value && is_compatible_type<BasicJsonType, uncvref_t<T>>::value;
#endif
template <typename T, typename BasicJsonType, typename U = uncvref_t<T>>
struct json_compatible_type
{
static constexpr auto value = !is_basic_json<U>::value && is_compatible_type<BasicJsonType, U>::value;
};

///////////////////////////////////////////////////////////////////////////////
// is_c_string
Expand Down
15 changes: 11 additions & 4 deletions include/nlohmann/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <detail::CompatibleType<basic_json_t> T>
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);
}

/// @brief comparison: not equal
/// @sa https://json.nlohmann.me/api/basic_json/operator_ne/
bool operator!=(const_reference rhs) const noexcept
Expand All @@ -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<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
24 changes: 16 additions & 8 deletions single_include/nlohmann/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4091,10 +4091,11 @@ inline constexpr bool value_in_range_of(T val)
template<bool Value>
using bool_constant = std::integral_constant<bool, Value>;

#ifdef JSON_HAS_CPP_20
template <typename T, typename BasicJsonType>
concept CompatibleType = !is_basic_json<uncvref_t<T>>::value && is_compatible_type<BasicJsonType, uncvref_t<T>>::value;
#endif
template <typename T, typename BasicJsonType, typename U = uncvref_t<T>>
struct json_compatible_type
{
static constexpr auto value = !is_basic_json<U>::value && is_compatible_type<BasicJsonType, U>::value;
};

///////////////////////////////////////////////////////////////////////////////
// is_c_string
Expand Down Expand Up @@ -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 <detail::CompatibleType<basic_json_t> T>
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);
}

/// @brief comparison: not equal
/// @sa https://json.nlohmann.me/api/basic_json/operator_ne/
bool operator!=(const_reference rhs) const noexcept
Expand All @@ -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<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

0 comments on commit f68d2b1

Please sign in to comment.