-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
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
Return null object from object's const operator[] as well. #1658
Comments
I am not convinced, but I am willing to discuss. An extension of the |
Extending from_json for std::optional can be handled in user code, and in any case is out of scope for this request. Adding this would have negligible space overhead (just one json object), compared to the cost of the lookup, the cost of checking for the end iterator is negligible, and if I read the README correctly, this library is more about usability than performance anyways. If someone has a reason against this change, please comment it here, since I can't really think of one. |
Also, it doesn't introduce any traps, since mutating the result would be a compile error, and since the referred-to object would be static, there is no danger of a dangling reference. |
I have to agree that this would be a convenient addition. This has caused many bugs for us, because it is so easy to get wrong... I also wonder what the rationale is for |
So since nobody supplied any reason why this would be outright bad, should I just make a PR? |
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. |
Currently const operator[] on object asserts if the key is not found. This is inconsistent with the non-const version, which adds and returns a null object.
IMO it should be possible to return a const reference to a static null json object.
This wouldn't be a breaking change, since it's currently UB. Unlike the non-const version, this wouldn't add anything to the subscripted object and would instead return a reference to a static object, but that should be OK, since it can't be modified anyways.
Specific use case for which this would be helpful:
Currently this code would assert if Message is not present (Note that it is perfectly valid for non-const json objects), whereas with this feature, it would work as expected.
The text was updated successfully, but these errors were encountered: