mirror of
https://github.com/nlohmann/json.git
synced 2026-07-09 12:05:10 +00:00
Fix #3868: Remove operator!= to enable P2468R2 rewritten candidate synthesis
Under C++20 P2468R2, a hand-written operator!= suppresses the compiler's rewritten-candidate synthesis for operator==, preventing heterogeneous comparisons like `std::string s; json j; s == j;` from compiling. Fix by removing the hand-written operator!=, allowing the compiler to synthesize != as !(a==b) in all language modes (C++20 member functions and pre-C++20 friend functions). Behavior change: operator!= now returns !(a==b) unconditionally, including for special values like NaN and discarded. This means: - NaN != NaN now returns true (matches IEEE-754 semantics) - discarded != x now returns true for any x (matches !(discarded == x)) This also fixes underlying defects in previously-working code: - Restores direct == comparison for views vs json (reverts std::ranges::equal workaround added in PR #3950 to dodge this bug) - Re-enables std::string == json comparisons (uncomments check in unit-constructor1.cpp) Fixes: #3868, #3979 Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Signed-off-by: Niels Lohmann <mail@nlohmann.me>
This commit is contained in:
@@ -24622,17 +24622,6 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
||||
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
|
||||
{
|
||||
if (compares_unordered(rhs, true))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return !operator==(rhs);
|
||||
}
|
||||
|
||||
/// @brief comparison: 3-way
|
||||
/// @sa https://json.nlohmann.me/api/basic_json/operator_spaceship/
|
||||
std::partial_ordering operator<=>(const_reference rhs) const noexcept // *NOPAD*
|
||||
@@ -24738,10 +24727,6 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
||||
/// @sa https://json.nlohmann.me/api/basic_json/operator_ne/
|
||||
friend bool operator!=(const_reference lhs, const_reference rhs) noexcept
|
||||
{
|
||||
if (compares_unordered(lhs, rhs, true))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return !(lhs == rhs);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user