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

Should I include single-header after my to_json and from_json custom functions declaration? Why not? #1663

Closed
oktonion opened this issue Jul 7, 2019 · 7 comments
Labels
kind: question state: stale the issue has not been updated in a while and will be closed automatically soon unless it is updated

Comments

@oktonion
Copy link

oktonion commented Jul 7, 2019

So I was going through the code and can not get how the library find user-defined functions for conversion (to_json and from_json)?

Let me explain with code.

simple example could look like this:

#include <json.hpp>

namespace mynamespace
{
    class myclass{};

    void to_json(json&, const myclass&);
    void from_json(const json&, myclass&);
}

// if I put it there then I understand how it finds my to_json and from_json
// #include <json.hpp> 

void func()
{
    mynamespace::myclass val;
    json j = val;
    val = j.get<mynamespace::myclass>();
}

Before include of <json.hpp> there is no declaration of mynamespace::to/from_json so I expect any check in <json.hpp> like has_custom_to_json to fail. If not then how it works?

My guess is that all the magic is in is_detected_exact but I could not figure out how is it possible and what template trick is used?

P.S.: Sorry for multiple edits.

@oktonion oktonion changed the title Should I unclude single-header after my to_json and from_json custom functions declaration? Should I unclude single-header after my to_json and from_json custom functions declaration? Why not? Jul 7, 2019
@oktonion oktonion changed the title Should I unclude single-header after my to_json and from_json custom functions declaration? Why not? Should I include single-header after my to_json and from_json custom functions declaration? Why not? Jul 7, 2019
@nlohmann
Copy link
Owner

It's using ADL. The internals are from @theodelrieu - maybe they can share more insights.

@stale
Copy link

stale bot commented Aug 10, 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 Aug 10, 2019
@oktonion
Copy link
Author

Any comments from @theodelrieu ?

@stale stale bot removed the state: stale the issue has not been updated in a while and will be closed automatically soon unless it is updated label Aug 11, 2019
@theodelrieu
Copy link
Contributor

Hello, this is because of two-phase lookup, you can find a short but accurate overview here.

Basically, val = j.get<mynamespace::myclass>(); is the point of instantiation, i.e. the lookup's second phase. At that point, your from_json method is visible to the compiler, whereas it wasn't in the first phase.

@oktonion
Copy link
Author

oktonion commented Aug 15, 2019

@theodelrieu Is this behaviour required by C++ standard (which one?) or could there be some other approach used by compiler? As I see from RSDN and SO discussions many compilers fail to implement two-phase lookup properly, so I assume this library would not work in such case,

@jaredgrubb
Copy link
Contributor

Two-phase lookup was always the expectation, even pre-C++11. Some compilers (eg, MSVC) didn't do it quite right but at this point, I would expect all the C++14 and later compilers to definitely have it down.

@stale
Copy link

stale bot commented Sep 15, 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 Sep 15, 2019
@stale stale bot closed this as completed Sep 23, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: question 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

5 participants
@nlohmann @jaredgrubb @theodelrieu @oktonion and others