Boost logo

Boost :

Subject: Re: [boost] [c++-sig]Python binding for templated constructor
From: Jim Bosch (talljimbo_at_[hidden])
Date: 2010-05-26 00:23:57


On 05/25/2010 01:58 PM, Sandeep Gupta wrote:
> Hi,
> I have a class with a generic constructor as follows :
> class X
> {
> indexes m_idx;
> template<class strategy>
> X(){
> build_indexes<strategy>()
> };
>
> My problem is that I am not able to figure out the syntax to export this
> constructor. Is the above class not well defined ?.
>
>
>

That's a good question. I'm not sure if it's possible using
boost::python::init, but here's a possible workaround:

namespace bp = boost::python;

template <typename strategy>
static X construct_X() { // could also return registered smart pointer to X.
     return X<strategy>();
}

BOOST_PYTHON_MODULE(example) {
     bp::class_<X>("X", bp::no_init) // no_init keeps bp from trying to
call default ctor
         .def("__init__",
bp::make_constructor(&construct_X<concrete_strategy>))
         ;
}

I haven't tested that, but I think it should work. By the way, you'll
get more responses to Boost.Python questions at the Python/C++-sig
mailing list:

cplusplus-sig_at_[hidden]

Hope that helps!

Jim Bosch


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