导出模板功能

Max*_*rai 4 c++ python templates boost export

使用boost.python将模板函数从c ++导出到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>);
Run Code Online (Sandbox Code Playgroud)

用法:

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

怎么了?

GMa*_*ckG 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
Run Code Online (Sandbox Code Playgroud)

根本问题是函数类型不携带默认参数信息.那么我们如何模拟呢?基本上,通过重载:

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
Run Code Online (Sandbox Code Playgroud)

维护麻烦.幸运的是,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
Run Code Online (Sandbox Code Playgroud)

该类成员也存在该宏.这是在文档中,如果其中任何一个不清楚.