Skip to content
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

Precedence of to_json and builtin harmful #1617

Closed
ingo-loehken opened this issue May 31, 2019 · 2 comments
Closed

Precedence of to_json and builtin harmful #1617

ingo-loehken opened this issue May 31, 2019 · 2 comments
Labels
kind: bug state: stale the issue has not been updated in a while and will be closed automatically soon unless it is updated

Comments

@ingo-loehken
Copy link

ingo-loehken commented May 31, 2019

For a class that supports user-defined conversion to a const char* (compatible to string_type of nlohmann::json) and a to_json specialization, the expectation would be that the to_json function takes precedence.

class MyClass
{
public:
	operator const char*() const; // (1)
	template <typename jsonT, typename T> friend inline typename std::enable_if_t<std::is_same_v<MyClass,std::remove_const_t<std::remove_reference_t<T>>>> to_json(jsonT&, T&&); // (2)
};

nlohmann::json x{MyClass{}}; // (3)

Executing (3), I would expect that (2) is selected, but (1) takes precedence.

@igoloe
Copy link

igoloe commented Jun 2, 2019

Built with Visual Studio 2017 15.4.1

Rationale

The example is just a simplified version of a to_json template function catching all derived classes convertible to a base class. All those classes are legacy data classes. Therefore eliminating the error prone implicit user defined conversion operator is very expensive and out-of-scope.

I just tried to understand why ADL selects the user defined conversion via detail::to_json with string_type, but failed.

In all manual computations, the to_json overload in MyClass is the bestmatch candidate...

Maybe I got it wrong.

MyClass const obj;
auto const json = nlohmann::json{{"x",obj}}; // (4)

Here (4) ends up with string_type overload.

@ingo-loehken ingo-loehken changed the title Precedence of to_json andf builtin harmful Precedence of to_json and builtin harmful Jun 3, 2019
@stale
Copy link

stale bot commented Jul 3, 2019

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.

@stale stale bot added the state: stale the issue has not been updated in a while and will be closed automatically soon unless it is updated label Jul 3, 2019
@stale stale bot closed this as completed Jul 10, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: bug state: stale the issue has not been updated in a while and will be closed automatically soon unless it is updated
Projects
None yet
Development

No branches or pull requests

2 participants