导出模板函数

5 投票
2 回答
3534 浏览
提问于 2025-04-16 13:59

如何将C++中的模板函数正确导出到Python中,使用boost.python库?下面是代码:

template<typename T>
T getValue(const std::string &key, const T &defaultValue = T()) {}

// Export into some python class:
class_<ConfigManager>(...)
.def("GetValue", getValue<int>)
.def("GetValue", getValue<float>)
.def("GetValue", getValue<std::string>);

还有用法:

    print GetValue("width")
Boost.Python.ArgumentError: Python argument types in
    GetValue(ConfigManager, str)
did not match C++ signature:
    GetValue(ConfigManager {lvalue}, std::string, int)

哪里出错了?

2 个回答

0

你还可以为你的类添加另一个模板,这样就不需要为每种整数或浮点数类型单独编写或实例化了。

template<typename LinksT>
class Base {
public:
  virtual ~Base()  {}
  virtual Base* x() = 0;
};

#include <boost/python.hpp>
using namespace boost::python;

template<typename LinksT>
class BaseWrap : public Base<LinksT>, public wrapper<Base<LinksT> > {
public:
  virtual Base<LinksT>* x() { return this->get_override("x")(); }
};

template<typename LinksT>
void export_virtualfunction()
{
    class_<BaseWrap<LinksT>, boost::noncopyable>("Base", no_init)
        .def("x", pure_virtual(&Base<LinksT>::x), return_internal_reference<>())
        ;
}

BOOST_PYTHON_MODULE(test_template_python)
{
    export_virtualfunction<int>();
}
6

你应该看看相关的Boost文档,关于默认参数的内容。我在下面给你简单总结一下。


这里的问题是,默认参数是在调用函数时使用的,在C++中。如果去掉这些默认参数,你就能从Python的角度看出问题所在:

// this function *must* be called with two parameters
template<typename T>
T getValue(const std::string &key, const T &defaultValue) {}

class_<ConfigManager>(...)
.def("GetValue", getValue<int>) // two arguments!
.def("GetValue", getValue<float>) // Python has no idea about the defaults,
.def("GetValue", getValue<std::string>); // they are a C++ feature for calling

根本的问题是,函数的类型并不包含默认参数的信息。那么我们该如何模拟这个功能呢?其实就是通过重载来实现:

template<typename T>
T getValue(const std::string &key, const T &defaultValue) {}

template<typename T>
T getValueDefault(const std::string &key)
{
    // default available in C++,
    // transitively available in Python
    return getValue(key);
}

class_<ConfigManager>(...)
.def("GetValue", getValue<int>) // two arguments
.def("GetValue", getValueDefault<int>) // one argument
// and so on

这会让维护变得麻烦。不过幸运的是,Boost让这变得简单:

template<typename T>
T getValue(const std::string &key, const T &defaultValue) {}

// creates utility class x, which creates overloads of function y,
// with argument count as low as a and as high as b:
// BOOST_PYTHON_FUNCTION_OVERLOADS(x, y, a, b);

BOOST_PYTHON_FUNCTION_OVERLOADS(getValueIntOverloads, getValue<int>, 1, 2);

class_<ConfigManager>(...)
.def("GetValue", getValue<int>, getValueIntOverloads()) // one or two arguments

// and so on

这个宏也可以用于类的成员函数。如果有不明白的地方,可以在文档中查找。

撰写回答