c++ - 使用 boost.python 将通用 C++ 库绑定(bind)到 python

标签 c++ python generics templates boost

我想知道绑定(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/

相关文章:

java - Arduino CRC计算和串口读取

Java:参数化可运行

delphi - 为什么缺少可选参数会导致 "Incompatible types"错误

python - 将目录中的所有 JPG 文件复制到 Python 中的另一个目录?

python - 有效地使用 Acora/迭代器中结果是否存在?

python - 可以在字段集中使用的有序 ManyToManyField

generics - 将枚举转换为迭代器

c++ - 逻辑错误 : Unable to determine the cause of segmentation fault

c++ - 将一张 map 的内容附加和替换到另一张 map ?

c++ - CoInitialize() 在 C++ 中未被调用异常