Skip to content

Commit f68d2b1

Browse files
committed
Don't use concept. Causes compilation error on GCC 10
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.
1 parent ee57287 commit f68d2b1

3 files changed

Lines changed: 32 additions & 16 deletions

File tree

include/nlohmann/detail/meta/type_traits.hpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -684,10 +684,11 @@ inline constexpr bool value_in_range_of(T val)
684684
template<bool Value>
685685
using bool_constant = std::integral_constant<bool, Value>;
686686

687-
#ifdef JSON_HAS_CPP_20
688-
template <typename T, typename BasicJsonType>
689-
concept CompatibleType = !is_basic_json<uncvref_t<T>>::value && is_compatible_type<BasicJsonType, uncvref_t<T>>::value;
690-
#endif
687+
template <typename T, typename BasicJsonType, typename U = uncvref_t<T>>
688+
struct json_compatible_type
689+
{
690+
static constexpr auto value = !is_basic_json<U>::value && is_compatible_type<BasicJsonType, U>::value;
691+
};
691692

692693
///////////////////////////////////////////////////////////////////////////////
693694
// is_c_string

include/nlohmann/json.hpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3694,12 +3694,19 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
36943694

36953695
/// @brief comparison: equal
36963696
/// @sa https://json.nlohmann.me/api/basic_json/operator_eq/
3697-
template <detail::CompatibleType<basic_json_t> T>
3697+
template <typename T>
3698+
requires detail::json_compatible_type<T, basic_json_t>::value
36983699
bool operator==(T rhs) const noexcept
36993700
{
37003701
return *this == basic_json(rhs);
37013702
}
37023703

3704+
/// @sa https://json.nlohmann.me/api/basic_json/operator_eq/
3705+
bool operator==(std::nullptr_t rhs) const noexcept
3706+
{
3707+
return *this == basic_json(rhs);
3708+
}
3709+
37033710
/// @brief comparison: not equal
37043711
/// @sa https://json.nlohmann.me/api/basic_json/operator_ne/
37053712
bool operator!=(const_reference rhs) const noexcept
@@ -3726,9 +3733,9 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
37263733

37273734
/// @brief comparison: 3-way
37283735
/// @sa https://json.nlohmann.me/api/basic_json/operator_spaceship/
3729-
template<typename ScalarType>
3730-
requires std::is_scalar_v<ScalarType>
3731-
std::partial_ordering operator<=>(ScalarType rhs) const noexcept // *NOPAD*
3736+
template <typename T>
3737+
requires detail::json_compatible_type<T, basic_json_t>::value
3738+
std::partial_ordering operator<=>(T rhs) const noexcept // *NOPAD*
37323739
{
37333740
return *this <=> basic_json(rhs); // *NOPAD*
37343741
}

single_include/nlohmann/json.hpp

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4091,10 +4091,11 @@ inline constexpr bool value_in_range_of(T val)
40914091
template<bool Value>
40924092
using bool_constant = std::integral_constant<bool, Value>;
40934093

4094-
#ifdef JSON_HAS_CPP_20
4095-
template <typename T, typename BasicJsonType>
4096-
concept CompatibleType = !is_basic_json<uncvref_t<T>>::value && is_compatible_type<BasicJsonType, uncvref_t<T>>::value;
4097-
#endif
4094+
template <typename T, typename BasicJsonType, typename U = uncvref_t<T>>
4095+
struct json_compatible_type
4096+
{
4097+
static constexpr auto value = !is_basic_json<U>::value && is_compatible_type<BasicJsonType, U>::value;
4098+
};
40984099

40994100
///////////////////////////////////////////////////////////////////////////////
41004101
// is_c_string
@@ -22913,12 +22914,19 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
2291322914

2291422915
/// @brief comparison: equal
2291522916
/// @sa https://json.nlohmann.me/api/basic_json/operator_eq/
22916-
template <detail::CompatibleType<basic_json_t> T>
22917+
template <typename T>
22918+
requires detail::json_compatible_type<T, basic_json_t>::value
2291722919
bool operator==(T rhs) const noexcept
2291822920
{
2291922921
return *this == basic_json(rhs);
2292022922
}
2292122923

22924+
/// @sa https://json.nlohmann.me/api/basic_json/operator_eq/
22925+
bool operator==(std::nullptr_t rhs) const noexcept
22926+
{
22927+
return *this == basic_json(rhs);
22928+
}
22929+
2292222930
/// @brief comparison: not equal
2292322931
/// @sa https://json.nlohmann.me/api/basic_json/operator_ne/
2292422932
bool operator!=(const_reference rhs) const noexcept
@@ -22945,9 +22953,9 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
2294522953

2294622954
/// @brief comparison: 3-way
2294722955
/// @sa https://json.nlohmann.me/api/basic_json/operator_spaceship/
22948-
template<typename ScalarType>
22949-
requires std::is_scalar_v<ScalarType>
22950-
std::partial_ordering operator<=>(ScalarType rhs) const noexcept // *NOPAD*
22956+
template <typename T>
22957+
requires detail::json_compatible_type<T, basic_json_t>::value
22958+
std::partial_ordering operator<=>(T rhs) const noexcept // *NOPAD*
2295122959
{
2295222960
return *this <=> basic_json(rhs); // *NOPAD*
2295322961
}

0 commit comments

Comments
 (0)