[C++-sig] Re: Custom WeakPtrs and "None"

David Abrahams dave at boost-consulting.com
Fri Nov 19 23:17:26 CET 2004

haller at ableton.com (Stefan Haller) writes:

> David Abrahams <dave at boost-consulting.com> wrote:
>> haller at ableton.com (Stefan Haller) writes:
>> > Yes, I don't want the Python variable itself to be set to None
>> > literally, I know there's no way to do that.  I just want it to
>> > *look* a bit more like None if the WeakPtr is NULL.  For example,
>> > I was hoping that it is possible to play tricks with overloading
>> > __cmp__ (or some such) to make the expression (weakptr == None)
>> > evaluate to True if weakptr is not None, but its pointee is NULL.
>> You could play that trick manually.  I would definitely *not* want
>> to do that automatically in the library.
> Could you please give me a hint how I would go about that?  I'm
> afraid I don't know what exactly has to be done.

Just define an __eq__ function that compares a weakptr<T> with T*:

bool eq(weakptr<X> x, X* other)
    return x.get() == other;

bool neq(weakptr<X> x, X* other)
    return x.get() != other;

        class_<X, ... >("X")
             .def("__eq__", eq)
             .def("__neq__", neq)

Dave Abrahams
Boost Consulting

More information about the Cplusplus-sig mailing list