Boost logo

Boost :

From: David B. Held (dheld_at_[hidden])
Date: 2002-10-02 13:45:46


"Larry Evans" <jcampbell3_at_[hidden]> wrote in message
news:3D9B312C.3080002_at_prodigy.net...
> [...]
> But this is what D.Held wants, isn't it. To be more explicit, let
> shared_ptr_const be the analog to SmartPtrToConst. Then define:
> [...]
> I think Held objects to this because Held wants to avoid "any needless
> copies". With the current shared_ptr, wouldn't the shared_ptr<foo>
> have to be converted to shared_ptr<foo const> via a copy constructor?

Larry, I think you're the only person who read my whole post. ;) Now I'm
tossing around the idea of a const-wrapper that stores a reference to
the pointer, like so:

template <class P>
class constT
{
public:
    typedef typename P::element_type element_type;

    constT(P const& p) : p_(p) { }

    element_type const* operator->() const { return get_pointer(p_); }
    element_type const& operator*() const { return *(get_pointer(p_)); }

    long use_count() ... etc.
private:
    P const& p_;
};

Then, you should (if I'm not mistaken) be able to do this:

typedef boost::shared_ptr<Bar> bar_ptr;

void foo(constT<bar_ptr> p)
{
    p->bar();
    p->baz(3); // Now caught!
}

You still have a copy, but it's only of the reference itself, which means
it's
a tiny cost regardless of the complexity of the pointer in question.

Dave


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk