2

I'm using pybind11 to create python bindings for a C++ library whose source I cannot change. It contains a class that defines member functions with rvalue reference arguments (eg T &&val). I am unable to create a binding to a member function with rvalue reference arguments but binding to a non-member function with identical arguments works as expected.

A simplified example looks like this:

struct Foo {
    // Cannot create a pybinding for this method.
    void print_ref(int &&v) const {
            std::cout << "Foo::print_ref(" <<  to_string(v) << ")" <<std::endl;
    }
};
// Pybinding for standalone function works as expected.
void print_ref(int&& val) {
        std::cout << "print_ref(" << to_string(val) << ")" << std::endl;
};

The pybind11 code looks like this:

PYBIND11_MODULE(refref, m) {
    py::class_<Foo>(m, "Foo")
    // Both of these attempts to create a pybinding FAILs with same error.
    .def("print_ref", &Foo::print_ref)
    .def("print_ref", (void (Foo::*) (int&&)) &Foo::print_ref);
    // This pybinding of standalone function is SUCCESSful.
    m.def("print_ref", &print_ref);
}

The compilation error on the first binding attempt is:

pybind11/bin/../include/site/python3.4/pybind11/pybind11.h:79:80: error: rvalue reference to type 'int' cannot bind to lvalue of type 'int'
        initialize([f](const Class *c, Arg... args) -> Return { return (c->*f)(args...); },
                                                                               ^~~~
pybind11/bin/../include/site/python3.4/pybind11/pybind11.h:1085:22: note: in instantiation of function template specialization 'pybind11::cpp_function::cpp_function<void, Foo, int &&,
      pybind11::name, pybind11::is_method, pybind11::sibling>' requested here
        cpp_function cf(method_adaptor<type>(std::forward<Func>(f)), name(name_), is_method(*this),
                     ^
refref.cpp:31:3: note: in instantiation of function template specialization 'pybind11::class_<Foo>::def<void (Foo::*)(int &&) const>' requested here
        .def("print_ref", &Foo::print_ref);

Any ideas on what I may be doing wrong? Since it works fine with non-member functions, I'm inclined to suspect a pybind11 issue but thought I would check here first.

Bob Barcklay
  • 1,584
  • 1
  • 15
  • 22

1 Answers1

2

Indeed, the problem comes from rvalues. I learned quite a few things from this SO answer and this blog post. There's a nice workaround : you can create a wrapper class that will redirect calls to the C++ library you cannot change, taking care of the rvalue with the std::move semantic.

#include <iostream>
#include <pybind11/pybind11.h>
#include <pybind11/numpy.h>

namespace py = pybind11;

struct Foo {
    void print_ref(int&& v) const {
            std::cout << "Foo::print_ref("  <<  +v << ")" <<std::endl;
    }
};


// This class will interface between PyBind and your library
class Foo_wrap{
private:
  Foo _mimu;
public:
  void print_ref(int v) const{
    _mimu.print_ref(std::move(v));
  }
};



PYBIND11_MODULE(example, m) {
     py::class_<Foo_wrap>(m, "Foo_wrap")
     .def(py::init())
     .def("print_ref", &Foo_wrap::print_ref);
}

That you can call in Python with

import example as fo

wr = fo.Foo_wrap()
wr.print_ref(2)
Christian
  • 1,162
  • 12
  • 21