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
gcc13.2 compile fail when using std::string to compare the json object #4165
Comments
This only happens with C++20 mode enabled, and appears to be a result of the updated C++ compare code. It works in one direction but not the other. This fails
This is good
|
I can confirm I get the same behavior running on ubuntu 23.04 with gcc 13.2. It looks like the reason why this only appears for c++20 is the template requirement for the operator==
If the 'requires' line is commented out it appears to use this version of the == operator for this case. I believe the reason why it works when lhs and rhs are swapped is because of the operator value_t() definition which lets it work with the std::string == operator. I'm not sure why this function isn't just setup to work with all compatible types which would work with the basic_json(rhs) conversion? |
I did not write this code myself, and did not find the time to read into these operators. Help is more than welcome here! |
Without having worked on the library details I cannot give good rationales, but I decided to try out changing the constraint so
The minimal example compiles (with GCC 13.2.1 on Arch) and the tests pass on There might be some details that I'm missing here, like unforeseen effects of this, so any input is appreciated. If this is okay I'll gladly make a PR for it. |
thats becauseIn GCC 13.1 and 13.2, stricter checks might be in place that catch potential issues earlier. It could be related to the handling of non-existing keys in a nlohmann::json object. Accessing a non-existing key with the operator[] in nlohmann::json returns a nullptr, which may not have a direct comparison overload with std::string in later versions of the library or compiler, I ALSO HAV THE CODE REFER TO THIS if u using .find(),this error wont occur in here } } |
When you write j["xxx"] == std::string("bar"), the operator== from the nlohmann::json library gets invoked, but due to the constrained template, it doesn't find a suitable match for a non-scalar right-hand side. This is different from std::string("bar") == j["xxx"], where the operator== for std::string comes into play, and the nlohmann::json object gets implicitly converted to std::string (if it can be), making the comparison possible. |
Description
Kindly refer to this link:
https://godbolt.org/z/c3fzjnqzG
,okay with gcc 12.3
failed with gcc 13.1 and 13.2
Reproduction steps
refer to description
Expected vs. actual results
expect using gcc13.2 can be compiled successfully
Minimal code example
The text was updated successfully, but these errors were encountered: