-
-
Notifications
You must be signed in to change notification settings - Fork 6.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
operator== documentation should show how to apply custom comparison function #1915
Comments
What I meant with "an alternative function" would be a user-defined function like bool my_equal(const json& lhs, const json& rhs)
{
switch (lhs.type())
...
} which implements whatever logic you would like. There is no extension point to change the behavior of |
I understand. What I am trying to suggest is this... let's say that a user has a custom scalar comparison in mind, like the example in the documentation page, or like this: inline bool is_same(double a, double b) noexcept
{
return a == b || (std::isnan(a) && std::isnan(b));
} Starting from that point, a user would like to evaluate whether two arbitrarily nested json objects are "equivalent," using the binary predicate. My suggestion is that the documentation include a working version of this function: bool my_equal(const json& lhs, const json& rhs)
{
// Uses `my_equal` to compare doubles.
switch (lhs.type())
...
} Maybe this could go on a "recipes" page if that is a better place for it. By the way, I think that actually writing this function would be facilitated if json had a |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
Comparing floating point with |
|
I also think we should by default use the default operator== for all types. |
Closed by merging #1984. |
What is the issue you have?
The documentation for
operator ==
mentions that double NaN values compare unequal, and says, "To compare floating-point while respecting an epsilon, an alternative comparison function could be used..." The example function is for comparing two doubles.In issue #514, about comparison between NaN values, @nlohmann wrote:
The documentation for
operator ==
should illustrate how a custom double comparison function could be applied recursively to all values in abasic_json
object. This would allow users to easily write their own function to evaluate twobasic_json
objects for equivalence.I think such a function could be written using
basic_json::flatten()
and comparing items pairwise with the std::equal algorithm.The text was updated successfully, but these errors were encountered: