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
In fact, if "writing data to the pointee of the result yields an undefined state", why not simply return a pointer to const in all cases, so the compiler will make sure I cannot modify the pointee.
your code is not wrong in the sense that it would not compile and do what you expect, the warning only states that you should not write to the pointer. A longer discussion about this can be found in #91.
Sorry, I've read #91 but I still don't get it.
According to the documentation, my code would compile but "yields an undefined state".
What does "undefined state" mean in this context?
Is value still valid after the assignment to p or not?
i.e. does value hold a json::number_integer_t equal to 42?
Sorry about the confusion. It is OK to write to the pointer if you are sure that the underlying JSON object did not change. Once that object changed, the pointer is not valid any more.
OK, that's much more clear now. Thanks for the clarification.
I suggest improving the documentation to make this point clear.
A good example could help:
json value = 17;
auto p = value.get_ptr<json::number_integer_t*>();
// writing through p is fine
// 'value' can be changed though the pointer
*p = 42;
assert( value == 42 );
// de-referencing p after an assignment to 'value' is still valid
// as long as the assignment didn't change the type of the json object
value = 1234;
assert( *p == 1234 );
// warning! *p is not valid after this assignment
// i.e. de-referencing p will yield undefined behavior
value = "doh!"
*p = 42; // <<< K-BOOM!
Activity
dariomt commentedon Oct 2, 2015
In fact, if "writing data to the pointee of the result yields an undefined state", why not simply return a pointer to const in all cases, so the compiler will make sure I cannot modify the pointee.
nlohmann commentedon Oct 3, 2015
Hi @dariomt,
your code is not wrong in the sense that it would not compile and do what you expect, the warning only states that you should not write to the pointer. A longer discussion about this can be found in #91.
Regards
Niels
dariomt commentedon Oct 5, 2015
Sorry, I've read #91 but I still don't get it.
According to the documentation, my code would compile but "yields an undefined state".
What does "undefined state" mean in this context?
Is
valuestill valid after the assignment topor not?i.e. does
valuehold ajson::number_integer_tequal to 42?nlohmann commentedon Oct 6, 2015
Sorry about the confusion. It is OK to write to the pointer if you are sure that the underlying JSON object did not change. Once that object changed, the pointer is not valid any more.
dariomt commentedon Oct 7, 2015
OK, that's much more clear now. Thanks for the clarification.
I suggest improving the documentation to make this point clear.
A good example could help:
nlohmann commentedon Dec 16, 2015
I'll add a comment to the function in a later commit. Thanks for the discussion!
fix for #127