From 4488378050072127870
X-Google-Thread: f78e5,480a31feed3c6a1b,start
X-Google-Attributes: gidf78e5,public
X-Google-Language: ENGLISH,ASCII-7-bit
Path: g2news1.google.com!news3.google.com!news.glorb.com!newsfeed.stueberl.de!newsfeed.vmunix.org!peer-uk.news.demon.net!kibo.news.demon.net!mutlu.news.demon.net!news.demon.co.uk!demon!stump.algebra.com!devnull
From: rdabrowa@poczta.onet.pl (Rafal Dabrowa)
Newsgroups: comp.std.c++
Subject: boost::shared_ptr revisited again
Date: Tue, 31 May 2005 17:00:06 GMT
Lines: 97
Sender: mail2news@demon.net
Approved: fjh@cs.mu.oz.au (Fergus Henderson , moderator of comp.std.c++)
Message-ID: <4299DE64.40403@poczta.onet.pl>
NNTP-Posting-Host: news.news.demon.net
Mime-Version: 1.0
Content-Type: text/plain; format=flowed; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit
X-Trace: news.demon.co.uk 1117558819 17581 158.152.254.254 (31 May 2005 17:00:19 GMT)
X-Complaints-To: abuse@demon.net
NNTP-Posting-Date: Tue, 31 May 2005 17:00:19 +0000 (UTC)
X-Robomod: STUMP, ichudov@algebra.com (Igor Chudov)
X-User-Agent: Mozilla Thunderbird 1.0 (X11/20041206)
X-Accept-Language: en-us, en
X-Virus-Scanned: amavisd-new at cs.mu.OZ.AU
X-Received: (from fjh@localhost)
	by mulga.cs.mu.OZ.AU (8.12.10+Sun/8.12.9/Submit) id j4VH060p001912;
	Wed, 1 Jun 2005 03:00:06 +1000 (EST)
X-Path: comp-std-cpp-robomod!not-for-mail
X-Delivered-to: std-c++@ucar.edu
X-Authentication-Warning: mulga.cs.mu.OZ.AU: fjh set sender to devnull@stump.algebra.com using -f
X-Newsgroups: comp.std.c++
Xref: g2news1.google.com comp.std.c++:4931


===================================== MODERATOR'S COMMENT: 
 If your message is rejected, the notice is sent to you by the moderation software, not directly from a human.  If your address is munged, you won't see it.


===================================== END OF MODERATOR'S COMMENT
I have posted my news about one week ago, it still doesn't appear on the 
newsgroup.
I'm not sure, what is reason for that -- I haven't received any answer, 
maybe because
of my e-mail address was spammers-protected (although my post contained 
signature with
way of decoding my address). I guess that my motivation of my proposal
was not enough strong. So, I want to afford to rewrite my post and 
describe my
motivation more detailed.

---------------

Boost library contains smart_ptr classes family, which likely becomes a part
of C++ standard library in future. I want to take a closer look at 
shared_ptr
class. This class is very useful in wide range of applications. It 
simplifies a
truly polimorphic programming. Containers of polymorphic objects
contain usually pointers to these objects. Replace a pointer by
shared_ptr takes off head pointers deletion. Shared pointers simplify
also maintenance of a data shared among objects. Shared pointers
are similar (in some aspects) to object references known from
Java. Although full implementation of Java object references is not
possible in C++, but at least a quite safe surrogate of them can we
have using shared_ptr objects.

    Up to be widely used, shared_ptr class should be easy in
use and as safe as possible.
    Although quite good, the shared_ptr class has some disadvantages.
First of all, with its "take ownership of pointer" property, it provides
rather moderate level of abstraction. It does not ensure that
the pointer will be always correct and gives programmer the
opportunity to introduce difficult bugs in program.

    It would be better to allocate the pointer by the class itself.
This would ensure that the pointer is always allocated correctly.
For example, the class may provide a method named "New".
The method would release previous pointer and allocate new one.

Of course, sometimes allocation of an object of a derived class is needed.
Also, sometimes a programmer needs to pass constructor parameters.
So, method "New" would be rather a template, like this:

    template <class X, class A>
    shared_ptr<T>& New(A arg);

This method would create an object of type X using its
constructor with one argument (arg).
Similar overloaded methods might create objects with two, three
arguments and so forth.

Use of "New" method rather than pass allocated pointers has
one more advantage: it makes harder to write code which
may cause memory leaks. Current implementation causes
memory leaks in cases when exception occurs between an object
allocation and pass it to shared_ptr object.

Some minor problems: I see that shared_ptr class does not have
any obvious method for check, whether it is set to 0. It would be
nice to have something like:

    bool is_nil() const;

Another method would allow to set the pointer value to 0, e.g.:

    void set_to_nil();

This would work like reset() without arguments, but, in my
opinion, the function name describes better what the function does.

One more method might simplify implementation of a
"copy on write" technique, i.e. creation of a separate copy
of object when it is about to change. For example, a method:

    void own_copy();

It might work as follows: if the object is shared among more than
one pointer, the function would create own copy of the object.
If the pointer is the only owner of the object, function would do
nothing.


Rafal Dabrowa

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]



