-
-
Notifications
You must be signed in to change notification settings - Fork 6.8k
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
from_json declaration order and exceptions #561
Comments
I can confirm you observation, but I also have no idea. Maybe a language lawyer like @gregmarr or @theodelrieu knows more about this. |
I'm far away from being a language lawyer, I will take a look, this looks really weird. EDIT: This is related to the I don't have a lot of times these days, but I'll try to investigate more. Meanwhile, if someone had a problem related to |
Well well well, there is something fishy, looks like a dark corner of C++. I have no idea why, but it seems that during the evaluation of the first This method has a I printed the value of the That's weird, and if you add the prototype The quick way of fixing this, is removing the |
I posted a question on SO last week. I'll try to explain what happens (I may have some details wrong, don't hit me) When the compiler sees the line Since However, the compiler seems to perform another instantiation once But, the There are 3 things we can do to resolve that issue:
The first solution would make your code work, but it still is undefined behaviour. And people caring about The second would make your code fail at compile-time, but the I think the last one is the only viable solution, you should either declare your prototypes before defining your methods, or put the definition in the correct order. Hope I was clear enough. |
My preference would be for something like this to cause a compiler error or warning, but I don't understand this library or the C++ standard well enough to properly evaluate the options you provided. Regardless, adding some docs explaining this issue would be great. |
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. |
@theodelrieu Can we close this? |
We should an entry in the documentation about the definition order. Other than that, I don't think we can do anything. |
Hm. What would be a proper line to add to the documentation? |
Something along those lines: Be careful with about the definition order of from/to_json. If a type B has a member of type A, you MUST define |
Thanks a lot! |
I am running into an issue where if a from_json function I wrote does a conversion that uses another from_json function I wrote, whether or not I can catch exceptions from nlohmann::json depends on the order of the from_json functions. Repro code below:
My test.json file is below:
In the above example, the output is:
If I switch the order of the from_json functions, the output is as expected:
I am pretty new to C++11 and C++ exceptions, so sorry if the reason for this is obvious.
My platform/compiler is OSX 10.11.6 with AppleClang 8.0.0.8000042 and I tested this using json version 2.1.1.
The text was updated successfully, but these errors were encountered: