Boost logo

Boost :

From: Dave Abrahams (abrahams_at_[hidden])
Date: 1999-12-05 13:48:37

>>> Yes. This is a major flaw in my idea. It's hard to do much with a class
>>> without a default constructor in C++; the standard containers seem to insist
>>> on one.
>> Right. That's why I am disinclined to make your "strong pointer"
>> the default Boost version. It seems you can get what you need by
>> just inheriting from shared_ptr and making the dangerous operations
>> private. Perhaps we can do that in boost as well.
> An alternative is to have the default constructor construct an instance of
> the pointed-to type with (new and) its default constructor. I'm not sure if
> that's a good idea either; seems it might be a bit inefficient.

Or you could have a family of template member function constructors which
forward their arguments on to the type being constructed. That would
*enforce* non-null initialization and deal with some nasty lurking
sequence-point issues, e.g.:

void f( smart_ptr<X>(new X), smart_ptr<X>(new X) ); // leak!!

so the constructors might be:

template <class X>
class smart_ptr {
    smart_ptr() : px(new X) {}

    template <class A1>
    smart_ptr(const A1& a1) : px(new X(a1)) {}

    template <class A1, class A2>
    smart_ptr(const A1& a1, const A2& a2) : px(new X(a1, a2)) {}

    ... (pick a reasonable upper limit on args).


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