-
Notifications
You must be signed in to change notification settings - Fork 260
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
[SUGGESTION] Null(ptr/opt) coalescing for safer pointer dereferencing and more pleasant optional usage #320
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
Comments
I think |
I think we had a discussion where Herb talked about how he talked with the C# team and that there were issues with having the nullable support throughout the language, and that he didn't want to tackle that just yet. I tried searching for it but couldn't find it. |
That's #27 (comment). |
Thanks @JohelEGP! |
I can see "plumbing nullability through the C# language" as being very different than "syntactic sugar for dealing with null/optional". However, it does have some pitfalls that need to be dealt with as far as short circuiting and single execution that could be difficult to deal with in the conversion to C++1. I could also see wanting to combine nullability with the general concept of using |
This issue comment is related to this topic. So it seems C++2 will use |
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
The canonical case
https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#i12-declare-a-pointer-that-must-not-be-null-as-not_null
Smart pointers can be
nullptr
. Dereferencing them is therefore always dangerous, and the following is the main way of guaranteeing safety:This is a pain that stems from two issues:
Optional sugar
Modern languages like Typescript and Rust provide ways to really nicely work with optional types using different forms of null coalescing to values.
Typescript:
The last one, in particular, allows for method chaining with early-exit if the return is undefined at any point along the chain.
This sugared way of working with optional types means that there is basically no reason not to do so whenever it makes sense because the barrier to usage is so low. The TSC tooling in particular makes it a breeze to use these things in a modern text editor, doing stuff like converting
.
to?.
if the left hand side of the dot access is an optional type. It's awesome.The proposal
Two ideas (can be decoupled).
IMO this would make working with both boxed values and optional types a real pleasure compared to where we are now in C++.
Other options
not_null_shared_ptr
andoptional_shared_ptr = std::optional<std::shared_ptr>
Tooling support
One could easily imagine a clang-tidy-like rule that checks for unchecked access to smart pointers and suggests changing
->
to?->
. This would make finding and preventing potential segfaults an automated process, with the only manual effort going into handling the nullopt cases of the optional dereferencing operator.Let me know if this sounds intriguing and I can work on a PR!
The text was updated successfully, but these errors were encountered: