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

Question about get_ptr() #127

Closed
dariomt opened this issue Oct 2, 2015 · 6 comments
Closed

Question about get_ptr() #127

dariomt opened this issue Oct 2, 2015 · 6 comments

Comments

@dariomt
Copy link
Contributor

dariomt commented Oct 2, 2015

I see the following in the documentation of get_ptr()

"@warning Writing data to the pointee of the result yields an undefined state."

IIUC this means that the following code yields an undefined state:

json value = 17;
auto p = value.get_ptr<json::number_integer_t*>();
*p = 42;

i.e. I cannot modify the integer value inside the json

Why is this so?

@dariomt
Copy link
Contributor Author

dariomt commented 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
Copy link
Owner

nlohmann commented 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
Copy link
Contributor Author

dariomt commented 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 value still valid after the assignment to p or not?
i.e. does value hold a json::number_integer_t equal to 42?

@nlohmann
Copy link
Owner

nlohmann commented 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
Copy link
Contributor Author

dariomt commented 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:

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!

@nlohmann
Copy link
Owner

I'll add a comment to the function in a later commit. Thanks for the discussion!

nlohmann added a commit that referenced this issue Dec 16, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants