我有一个C++函数计算一个大张量,我想通过pybind11返回到Python作为NumPy数组.
从pybind11的文档来看,似乎使用STL unique_ptr是可取的.在下面的示例中,注释掉的版本有效,而给定的版本在运行时编译但失败("无法将函数返回值转换为Python类型!").
为什么smartpointer版本失败了?创建和返回NumPy数组的规范方法是什么?
PS:由于程序结构和数组的大小,不希望复制内存,而是从给定的指针创建数组.内存所有权应由Python采用.
typedef typename py::array_t<double, py::array::c_style | py::array::forcecast> py_cdarray_t;
// py_cd_array_t _test()
std::unique_ptr<py_cdarray_t> _test()
{
double * memory = new double[3]; memory[0] = 11; memory[1] = 12; memory[2] = 13;
py::buffer_info bufinfo (
memory, // pointer to memory buffer
sizeof(double), // size of underlying scalar type
py::format_descriptor<double>::format(), // python struct-style format descriptor
1, // number of dimensions
{ 3 }, // buffer dimensions
{ sizeof(double) } // strides (in bytes) for each index …Run Code Online (Sandbox Code Playgroud) 我需要为std :: vector创建一个shared_ptr,正确的语法是什么?
std::vector<uint8_t> mVector;
shared_ptr<std::vector<uint8_t>> mSharedPtr = &mVector;
Run Code Online (Sandbox Code Playgroud)
上面的代码不能编译.
谢谢.