SmartPtrs: Making Code Self-Describing. Do we need more SmartPtrs?

Hi boosters, Please consider the following code which we IMO too often see in other libraries: class Foo; Foo * Bar(Foo *); What can we say about this pretty C-like function? It gets pointer Foo and returns pointer Foo. That's all we can be sure about it. But that's not enough to start working with it. But without looking at documentation we can't say whether we should delete these pointers or function Bar will handle it. What if even documentation says anything about it, what to do then? Thanks C++, we've got auto_ptr. Now we explicitly mark that who should care about pointers. So, if we have auto_ptr<Foo> Bar(auto_ptr<Foo>) we do know how to work with it even without looking at the documentation. Example 1: We are the developer of function Foo. And we need to mark somehow that returned pointer is property of Foo internals and it shouldn't be deleted by the client function. We can achieve this using weak_ptr, but what if Foo internals doesn't have any shared_ptr's ? What to return then? Plain pointer? No Way! We need a way to explicitly say that we return copy of pointer that can't be deleted and no synchronization is needed. So, IMO We need another smart pointer type. Unfortunately, I didn't think of a good name for it, so it's still unnamed, but I think you understand what I am proposing. Example 2: We are developer of function Foo again. And now we need to mark that pointer argument is needed only for time of execution of Foo (it wasn't collected anywhere), and user cares about deallocation. How are we going to write such code and make it self-describing at the same time? Plain pointer? No Way! That would be a C-style, but we need something like C++-style... This time we don't have even one std or boost candidate for such job. So we need one more smart_ptr. Again, this one needs a good name too. --- So, anybody interested? Do we need them? Or we've already have some alternative solution I didn't find? Please let me know. Just wanted make code a bit more self-describing, there is no place for dumb pointers at all in high-level C++ programming. And Is there any place for such smart pointers in Boost? As always any comments are welcome. P.S. Description of boost.smart_ptr on libraries page still says that there are 5 smart pointers, instead of actual 6. -- Pavel Chikulaev

So, anybody interested? Do we need them? Or we've already have some alternative solution I didn't find?
I am sorry. I do not have a time to read through your post, but, did you take a look on policy based smart pointer solution? There is one up for review (soon I hope) policy_ptr in vault. Gennadiy

On 04/25/2005 10:52 AM, Gennadiy Rozental wrote: [snip]
I am sorry. I do not have a time to read through your post, but, did you take a look on policy based smart pointer solution? There is one up for review (soon I hope) policy_ptr in vault.
I couldn't find it in http://boost-sandbox.sourceforge.net/vault/ . Did you mean: http://cvs.sourceforge.net/viewcvs.py/boost-sandbox/boost-sandbox/boost/poli... ? Even there, I can't tell whether it's the smart_ptr.hpp in that directory or in the latest subdirectory. I assume it's in that directory because that has the latest change by turkanis (2 mos.) David? Jonathan?

On Monday 25 April 2005 17:35, Pavel Chikulaev wrote:
Example 1:
We are the developer of function Foo. And we need to mark somehow that returned pointer is property of Foo internals and it shouldn't be deleted by the client function. We can achieve this using weak_ptr, but what if Foo internals doesn't have any shared_ptr's ? What to return then? Plain pointer? No Way! We need a way to explicitly say that we return copy of pointer that can't be deleted and no synchronization is needed.
First thought is to return a reference instead of a pointer. If there really is need to sometimes return a null pointer, one could use boost::optional with a reference.
Example 2:
We are developer of function Foo again. And now we need to mark that pointer argument is needed only for time of execution of Foo (it wasn't collected anywhere), and user cares about deallocation. How are we going to write such code and make it self-describing at the same time? Plain pointer? No Way! That would be a C-style, but we need something like C++-style... This time we don't have even one std or boost candidate for such job.
Isn't this the same thing but this time the object being passed into the function instead of out of it? Anyhow, I think the same solution applies. For the case of null pointers, there is even the additional possibility of overloading. Uli
participants (4)
-
Gennadiy Rozental
-
Larry Evans
-
Pavel Chikulaev
-
Ulrich Eckhardt