[C++-sig] Implicitly converting HeldTypes from Derived to Base?

Chad Austin caustin at gmail.com
Wed Aug 25 00:35:14 CEST 2004


I have a situation like this:


// foo.cpp

#include <boost/intrusive_ptr.hpp>
#include <boost/python.hpp>
using namespace boost;
using namespace boost::python;


class RefCounted {
public:
    RefCounted() {
        _count = 0;
    }

    virtual ~RefCounted() {
    }

    int _count;
};

void intrusive_ptr_add_ref(RefCounted* ptr) {
    ++(ptr->_count);
}

void intrusive_ptr_release(RefCounted* ptr) {
    if (--(ptr->_count) == 0) {
        delete ptr;
    }
}


class A : public RefCounted { };
class B : public A { };

typedef intrusive_ptr<A> APtr;
typedef intrusive_ptr<B> BPtr;


void foo(APtr a) {
}


BOOST_PYTHON_MODULE(foo) {
    class_<A, APtr>("A");
    class_<B, BPtr, bases<A> >("B");
    def("foo", &foo);

    // Without implicitly_convertible, foo(B()) fails with:
    //
    // Traceback (most recent call last):
    //   File "<stdin>", line 1, in ?
    // Boost.Python.ArgumentError: Python argument types in
    //     foo.foo(B)
    // did not match C++ signature:
    //     foo(boost::intrusive_ptr<A>)
    implicitly_convertible<BPtr, APtr>();
}



Is there any way that implicitly_convertible call from the Derived
HeldType to the Base HeldType can be made automatic?

Thanks,
Chad



More information about the Cplusplus-sig mailing list