开发者

binding generic c++ libraries to python with boost.python

开发者 https://www.devze.com 2023-01-06 00:14 出处:网络
I would like to know what\'s the process, when binding C++ libraries that are written with in a generic way.

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 开发者_如何学JAVAa template generated class ?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消