nlohmann / json

JSON for Modern C++
https://json.nlohmann.me
MIT License
42.38k stars 6.67k forks source link

Change `operator==` overload that constrains to `is_scalar_v` to constrain to compatible types. #4181

Open fsandhei opened 12 months ago

fsandhei commented 12 months ago

https://github.com/nlohmann/json/issues/4165 shows an issue with the overload resolution when attempting to compare a std::string with json from left to right. This can be fixed by changing the constraint to instead of focusing on scalar types, we constrain the comparison operator to "compatible types", in par with the catch-call constructor.

Pull request checklist

Read the Contribution Guidelines for detailed information.

Please don't

github-actions[bot] commented 12 months ago

🔴 Amalgamation check failed! 🔴

The source code has not been amalgamated. @fsandhei Please read and follow the Contribution Guidelines.

github-actions[bot] commented 12 months ago

🔴 Amalgamation check failed! 🔴

The source code has not been amalgamated. @fsandhei Please read and follow the Contribution Guidelines.

coveralls commented 12 months ago

Coverage Status

coverage: 100.0%. remained the same when pulling 65b9b38ff33829b43d15488d84413233a34d63fb on fsandhei:fix/equality-operator-for-compatible-types into 0457de21cffb298c22b629e538036bfeb96130b7 on nlohmann:develop.

nlohmann commented 12 months ago

Can you please try to amalgamate with an older version of astyle, like astyle 3.2? There was a breaking change in astyle which makes the output differ unfortunately.

fsandhei commented 12 months ago

Can you please try to amalgamate with an older version of astyle, like astyle 3.2? There was a breaking change in astyle which makes the output differ unfortunately.

Sure, I'll do it some time later today after work.

fsandhei commented 12 months ago

First, sorry, @nlohmann, for the trouble I'm causing here.

So, I tried amalgamating again. There seems to be some issues or inconsistencies with the astyle version 3.2.1 from the AUR versus 3.2.1-build from apt:

Would you want me to perhaps unstage all the other unrelated files that got affected by this?

github-actions[bot] commented 12 months ago

🔴 Amalgamation check failed! 🔴

The source code has not been amalgamated. @fsandhei Please read and follow the Contribution Guidelines.

nlohmann commented 12 months ago

This is what I am using locally (patched Makefile):

# call the Artistic Style pretty printer on all source files
pretty:
    /Users/niels/Downloads/astyle/build/astyle \
        --style=allman \
        --indent=spaces=4 \
        --indent-modifiers \
        --indent-switches \
        --indent-preproc-block \
        --indent-preproc-define \
        --indent-col1-comments \
        --pad-oper \
        --pad-header \
        --align-pointer=type \
        --align-reference=type \
        --add-braces \
        --convert-tabs \
        --close-templates \
        --lineend=linux \
        --preserve-date \
        --suffix=none \
        --formatted \
       $(SRCS) $(TESTS_SRCS) $(AMALGAMATED_FILE) $(AMALGAMATED_FWD_FILE) docs/examples/*.cpp

with Astyle 3.1.

fsandhei commented 12 months ago

Thank you for so quick response. The amalgamation seems happy now.

nlohmann commented 12 months ago

Thank you for so quick response. The amalgamation seems happy now.

Thanks for your effort!

fsandhei commented 11 months ago

I see that the GCC 10 CI run failed. It seems to be not able to parse the concept, which is odd as it should be supported in GCC 10.

For the sake of getting this in and have the least amount of hassle I don't use concept.

However I was pondering on the idea of having a concept which could replace these type traits definitions for recognizing "compatible types". I tinkered with this simple concept below. It worked perfectly fine. I don't know if it is missing any details, though. Would appreciate hearing your opinion on this, as I'm far from an expert :)

#ifdef JSON_HAS_CPP_20
   #include <concepts>
    template <typename T, typename BasicJsonType, typename U = uncvref_t<T>>
    concept CompatibleType = requires(const U& type, BasicJsonType& j) {
        { to_json(j, type) } -> std::convertible_to<void>;
    };
#endif

The use of this would be quite simple, as we'd then can constrict constructors / operators with this concept:

template <CompatibleType<basic_json_t> T>
operator==(T rhs) const noexcept
{
   ...
}

EDIT: I realize I goofed myself. It is supported but JSON_HAS_CPP_20 is not defined because g++10 requires -std=c++2a. I'm able to get past this by also checking for __cpp_concepts to be defined at where we define the concept.

fsandhei commented 7 months ago

I'm not really sure what is Codacy is actually complaining about. The struct member is being used in the type trait. It might be a bug in Codacy. Do you have any suggestions @nlohmann?

nlohmann commented 7 months ago

I'm not really sure what is Codacy is actually complaining about. The struct member is being used in the type trait. It might be a bug in Codacy. Do you have any suggestions @nlohmann?

Indeed looks like a false positive. I set it to "ignore".

github-actions[bot] commented 7 months ago

🔴 Amalgamation check failed! 🔴

The source code has not been amalgamated. @fsandhei Please read and follow the Contribution Guidelines.