|
Boost : |
From: Pavel Chikulaev (pavel.chikulaev_at_[hidden])
Date: 2005-04-25 10:35:40
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
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk