-
-
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
Conversion to user type containing a std::vector not working with documented approach #1511
Comments
I think I understand the problem you're having. The main difference between Since you default constructs some When using |
You're right, it has to do with the default constructor populating the array. What does Thanks for your help. |
|
ok, true it's not constructing, I am confused by the prepending of precisely one At the moment doing any of the following pieces of code will lead to json j;
A a;
a.a.push_back(1.0); // a.a == [0.0, 1.0]
for(int i=0; i<10; i++){
j=a;
a=j;
}
// a.a == [0.0, 0.0, 0.0, 0.0, 0.0 .... , 1.0] or for(int i=0; i<10; i++){
j=a;
a=j.get<A>();
}
// a.a == [0.0, 0.0, 0.0, 0.0, 0.0 .... , 1.0] or for(int i=0; i<10; i++){
j=a;
j.get_to(a);
}
// a.a == [0.0, 1.0, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0, ....] While all these behaviours result in |
I agree this is very confusing. As of now, I think this is a bug, we should treat the second parameter as an output parameter, therefore overwriting the previous value like we do for |
🔖 release item in #1555 |
When performing an assignement from
json
to a user defined type, vectors inside this object get appended. I've included a working example of the bug at the end of this issue but this is a smaller summary:I have read a bit about the problems with doing
std::vector<int> v=j
and that theget
method ofjson
should be used, however these issues seem to also affect thej.get_to(v)
method. This means that when following the documentation for converting user defined types very strange run-time behaviours arise with no compile time indication that anything may go wrong.This can be fixed by replacing the
from_json
function to:Not pretty and a bit brittle but simple enough.
Possible solutions
Am I doing something silly here that is causing this?
If I am not I see 3 options:
get_to
method...system
Small Compilable Example
Output
The text was updated successfully, but these errors were encountered: