-
Notifications
You must be signed in to change notification settings - Fork 738
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
not_null<unique_ptr<>> doesn't work #89
Comments
One additional sticking point here is what will get() return? Can't return the unique_ptr by value. I would say unique_pointer<>::pointer. How about for shared_ptr? That doesn't have a ::pointer typedef. Of course that can return by value, but would incur the cost of locking and incrementing. |
The real question that needs to be asked here is should If so,
With regards to |
By using
|
It looks to me that there are two different issues being discussed in this thread. The first issue is whether or not not_null should contain its T, or just a reference to it. not_null is designed to contain its T. In the case of a raw pointer, that's pretty trivial and (hopefully) not controversial. For a class type, like unique_ptr or array_view, it should also be contained within the not_null, by value. The second issue is whether not_null should forward functions for the T it contains. The answer here is yes. not_null is conceptually a wrapper that maintains an invariant for the T, but other than that, the usual behaviors and operations for the T should be available on not_null. |
What if we want to differentiate invariant not_null that must always be not null from not_null argument that we wan't the function to get. This means that I would like to have sth like not_null_arg<pointer/smart_ptr> that is checked everytime it is created or transforment into that underlying variable is not null. In other words I would like to be should that my std::shared_ptr or std::unique_ptr is not null when it is passed to any of my function:
now if I am willing to get the unique_ptr out of arg I should be able to do std::move(p_arg.arg) out of it. The class could be something like this.
|
See F.7 for passing pointers as arguments. |
Ok, but what about the case where I want to use std::shared_ptr to pass shared ownership to a function and I want to make sure (from function POV) that it will not be empty? |
@netrix |
I think the initialization of injected objects would be good example for that. Consider many classes that have constructors or functions (setters) that accepts std::shared_ptr or std::unique_ptr that should be allways a valid pointer (not null). Eg. Foo(std::unique_ptr p_injectedObject). Since we want inject something to an object we are doing it via smart_ptr because this object should own it. Now I am not aware of any mechanism that will allow me to be should that the pointer is not null, because passing a nullptr in such scenario is a nonsense so I would like to catch it as fast as possible. This is why I would like to have sth like not_null for smart pointers since there is no other way (or I am not aware of) of doing it. |
@netrix |
If I understand correctly it doesn't but I don't know how to express them in code. |
I'm experimenting with not_null<> and smart pointers, and run into similar issues like the ones discussed here. Regarding unique_ptr, I see the problem as follows. unique_ptr<> is not copy-constructible, not_null<> is not move-constructible. It looks like a paradox, but intuitively speaking there is common ground. Just before a not_null<> goes out of scope, we could legally move from it, because there's no way to access it between the point of moving and the point of destruction (I hope this makes sense.) That's the case when not_null<> is returned from a function. I wonder if C++ can distinguish this situation from the common case of moving from an object. Is it possible to express the distinction in the type system? If so, we could allow the former and disable only the latter. |
FWIW, |
I cloned the GSL repo last night after reading the comments in |
Hi @ChrisChiasson, thanks for your comment. We are aware that it is possible to make a "not-null" wrapper type work with smart pointers. We are simply moving slowly and carefully here. The most common use-case we had for |
allows support of supporting std::unique_ptr based on @xaxxon's work fixes microsoft#89 fixes microsoft#550 fixes microsoft#651
allows support of supporting std::unique_ptr based on @xaxxon's work fixes microsoft#89 fixes microsoft#550 fixes microsoft#651
allows support of supporting std::unique_ptr based on @xaxxon's work fixes microsoft#89 fixes microsoft#550 fixes microsoft#651
allows support of supporting std::unique_ptr based on @xaxxon's work fixes microsoft#89 fixes microsoft#550 fixes microsoft#651
Maintainers' call: Close this out since |
Since guidelines specify: "not_null ... T can be any type for which ==nullptr is meaningful." this should work
The text was updated successfully, but these errors were encountered: