pybind / pybind11

Seamless operability between C++11 and Python

Home Page:https://pybind11.readthedocs.io/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[BUG]: How to bind a c array within a class function

Stoneplay opened this issue · comments

Required prerequisites

What version (or hash if on master) of pybind11 are you using?

2.11.1

Problem description

Due to the set_max_vel function of Engine class is not static function, so it will raise an error when I bind the the set_max_vel function like that. How to bind a c array which is in a function within a class?

Reproducible example code

// c++ code
#include <Eigen/Core>

class Engine: {
public:
    Engine();
    void set_max_vel(float *max_vel, int dof){
        max_velocity_.resize(dof);
	for (int i = 0; i < dof; i++){
            max_velocity_[i] = (double)max_vel[i];
	}
    }
private:
    Eigen::VectorXd max_velocity_;
}

// binding code
#include <pybind11/pybind11.h>
#include <pybind11/numpy.h>

namespace py = pybind11;

py::class_<Engine>(m, "Engine")
    .def(py::init<>())
    .def("set_max_vel", [](py::array_t<float> buffer, int dof) {
        py::buffer_info info = buffer.request();
	Engine::set_max_vel(static_cast<float*>(info.ptr), dof); });

Is this a regression? Put the last known working version here if it is.

Not a regression