views:

53

answers:

1

I would like to know what's the process, when binding C++ libraries that are written with in a generic way.

Is there a posibility of binding a template class, or you can only bind only a template generated class ?

+2  A: 

You can only bind a generated class. However, it is possible to write a template function to export your class, and call this function for each concrete types you want to export. For example:

template<class T>
struct foo {};

template<class T>
void export_foo(std::string name) { 
    boost::python::class_<foo<T>>(name.c_str());
}

BOOST_PYTHON_MODULE(foo)
{
    export_foo<int>("foo_int");
    export_foo<std::string>("foo_string");
    //...
}

If it is not enough, you can also dive into meta programming (with Boost.MPL for example), to create typelists, and automatically call export_foo for all these types.

rafak
great answer, thx
lj8888