Boost logo

Boost :

From: Darin Adler (darin_at_[hidden])
Date: 2002-04-02 10:55:41

On Tuesday, April 2, 2002, at 12:01 AM, marchetti alessio wrote:

> The following idiom creates confusion (the ip variable is not used
> after the ctor call):
> int * ip = new int;
> shared_ptr<int> cp ( ip );
> The first idiom promotes better encapsulation: do you think that
> forcing this usage patterns through interface would make sense?
> It's possible to do that by changing the ctor [...] to
> explicit shared_ptr(T * const &p = 0): px(p), pn(p, deleter())
> {
> }
> and by adding an additional dummy ctor:
> explicit shared_ptr(T * &p): px(p), pn(p, deleter())
> {
> shared_ptr_ctor_needs_temporary_actual_parameter;
> }
> This change of course breaks source compatibility...

Some people might like this -- I would like it for my programs. But I
think it's a bit too subtle and tricky for it to be widely accepted.
Also note that it leaves a loophole for code like this:

     int * const ip = new int;
     shared_ptr<int> cp ( ip );

But that doesn't invalidate the idea, since this is probably far less
common than the idiom we are trying to avoid.

I think the idea is good, but probably not compelling enough to be worth
breaking compatibility for. One quality of implementation issue that
we'd want to look into is whether this gives error messages that are
easy to understand on various compilers.

     -- Darin

Boost list run by bdawes at, gregod at, cpdaniel at, john at