Boost logo

Boost :

From: John Torjo (john.lists_at_[hidden])
Date: 2004-10-18 02:53:19

> You can't accidentally omit the lock(), and shared_ptr<T> p( weak ) is
> invalid in an if statement.
> shared_ptr<T> val( weak ); does exactly what it is intended to do.

It does what it is documented to do. But I really fail to see any code
that would actually prefer this:

shared_ptr<T> val( weak );

over this:

shared_ptr<T> val = weak.lock();

Thus, people might use the former synax by mistake. At least, I did it ;)

Basically, actually my mistake was more subtle:

struct wnd {
    shared_ptr<...> p;
    // bug here: it should have actually been shared_ptr<...>
    wnd( weak_ptr<...> p) : p(p) {}

struct weak_wnd {
     weak_ptr<...> p;
     shared_ptr<...> to_wnd() const { return p.lock(); }

weak_wnd a;
// a.to_wnd():
// a shared pointer gets constructed, which is automatically converted
// to a weak_ptr, which then is converted to a shared_ptr
wnd b = a.to_wnd();

Had that constuctor not been public, this would have generated a
compile-time error.


John Torjo,    Contributing editor, C/C++ Users Journal
-- "Win32 GUI Generics" -- generics & GUI do mix, after all
-- v1.5 - tooltips at your fingertips (work for menus too!)
    + bitmap buttons (work for MessageBox too!)
    + tab dialogs, hyper links, lite html

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