【问题标题】:binding generic c++ libraries to python with boost.python使用 boost.python 将通用 c++ 库绑定到 python
【发布时间】:2010-07-08 15:59:06
【问题描述】:

我想知道在绑定以通用方式编写的 C++ 库时的过程。

是否可以绑定模板类,或者只能绑定模板生成的类?

【问题讨论】:

  • 我忘了说,我正在尝试为 Configurable Math Library (cmldev.org) 编写绑定

标签: c++ python generics templates boost


【解决方案1】:

您只能绑定生成的类。但是,可以编写一个模板函数来导出您的类,并为您要导出的每个具体类型调用此函数。例如:

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。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-19
    • 1970-01-01
    • 2011-12-10
    • 1970-01-01
    • 1970-01-01
    • 2016-02-11
    相关资源
    最近更新 更多