我想知道绑定(bind)以通用方式编写的 C++ 库时的过程是什么。
是否可以绑定(bind)模板类,或者只能绑定(bind)模板生成的类?
最佳答案
您只能绑定(bind)生成的类。但是,可以编写一个模板函数来导出您的类,并为您想要导出的每个具体类型调用此函数。例如:
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");
//...
}
如果这还不够,您还可以深入研究元编程(例如使用 Boost.MPL),创建类型列表,并自动为所有这些类型调用 export_foo。
关于c++ - 使用 boost.python 将通用 C++ 库绑定(bind)到 python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3205561/