class module_ : public object {
public:
PYBIND11_OBJECT_DEFAULT(module_, object, PyModule_Check)
template <typename Func, typename... Extra>
module_ &def(const char *name_, Func &&f, const Extra& ... extra) {
cpp_function func(std::forward<Func>(f), name(name_), scope(*this),
sibling(getattr(*this, name_, none())), extra...);
// NB: allow overwriting here because cpp_function sets up a chain with the intention of
// overwriting (and has already checked internally that it isn't overwriting non-functions).
add_object(name_, func, true /* overwrite */);
return *this;
}
}
以上代码摘自pybind11, class module_ 定义一个模块函数, 其def可以注册一个C++定义的函数,
使它能够暴露给python程序导入, 一个简单的例子
m.def("add_arrays", &add_arrays, "Add two NumPy arrays")
现在是,add_arrays 的数组元素参数是double, 现在想用模板扩展, 那么这个def,怎么写呢?
template <typename T>
py::array_t<T> add_arrays(typename py::array_t<T> input1, typename py::array_t<T> input2);
--
FROM 115.171.245.*