Non-nullable smart pointers by default #662
Closed
AbhinavK00
started this conversation in
Suggestions
Replies: 2 comments 1 reply
-
No. |
Beta Was this translation helpful? Give feedback.
0 replies
-
See F.7: For general use, take T* or T& arguments rather than smart pointers.
|
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Cpp2 has made working with dynamic memory easier/safer by providing smart pointers by default and has a goal to eliminate bugs/vulnerabilities due to use of NULL/nullptr by not having those keywords in cpp2.
But cpp2 has to interface with cpp and that means having to check for
nullptr
s in most cases.I propose that cpp2 should provide non-nullable smart pointers by default to make it easier when working with smart pointers. Guidelines Support Library has a class
gsl::not_null
which does the same. Maybe cpp2 can have its ownnot_null
and wrap smart pointers to achieve this. Another way would be have a cpp2::unique_ptr which ensures that it's not null.If the user wishes to work with nullable pointers, he can wrap the smart pointer in an optional. This would also allow user to use interface of optional for easier null handling.
(Is there a way to tell
std::optional
that nullptr value is same as the value not being present and therefore it can optimise away the bool discriminator?)Beta Was this translation helpful? Give feedback.
All reactions