You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Should I include single-header after my to_json and from_json custom functions declaration? Why not? #1663
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>namespacemynamespace
{
classmyclass{};
voidto_json(json&, const myclass&);
voidfrom_json(const json&, myclass&);
}
// if I put it there then I understand how it finds my to_json and from_json// #include <json.hpp> voidfunc()
{
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?
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?[/+]on Jul 7, 2019
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?[/+]on Jul 7, 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.
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.
@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,
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.
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.
Activity
[-]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?[/+][-]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?[/+]nlohmann commentedon Jul 11, 2019
It's using ADL. The internals are from @theodelrieu - maybe they can share more insights.
stale commentedon 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.
oktonion commentedon Aug 11, 2019
Any comments from @theodelrieu ?
theodelrieu commentedon Aug 12, 2019
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, yourfrom_jsonmethod is visible to the compiler, whereas it wasn't in the first phase.oktonion commentedon 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 commentedon Aug 16, 2019
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 commentedon 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.