pybind / pybind/pybind11

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

Open
#5,044 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

triage
Dominant language
C++
Stars
18k
Forks
2.3k
Avg merge
5d 17h
Merged PRs (30d)
10

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the reported binding with the Engine class, set_max_vel method, and the shown pybind11 binding code. Check how the instance method is invoked and how the py::array_t buffer is passed; done means the binding accepts the array and calls set_max_vel without the reported error.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp, python
Domain
api
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.