-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Rvalue reference parameters work in constructors, but not in other functions #1694
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Here is another example: #include <pybind11/pybind11.h>
namespace py = pybind11;
class C {
public:
C() = default;
void f(std::string &&) {}
};
PYBIND11_MODULE(frame_comparer, m) {
py::class_<C>(m, "C")
.def(py::init<>())
.def("f", &C::f);
} fails with:
Maybe a good way to solve this is to allow syntax along the lines of: ...
.def_with_this("f", [](C* this_, std::string argument) { return this_->f(std::move(argument)); }); where the first argument is always the this pointer. This is terser than the workaround of defining a whole wrapper class and cleaner than the other workaround of making a non member wrappper. |
What exactly is the solution to this issue (identified by the op)? I am also encountering similar error. Update: |
This pull request seems to fix the problem, but for some reason it hasn't been merged: |
Thanks for that note. A bit off topic, but I was wondering if you know how I can expose operator() from a c++ class to python. I tried:
This does not quite work. |
I'm not sure the constructor works either: I think behind the scenes the compiler's using the copy constructor. In my tests with move-only types, a constructor taking a rvalue reference to an uncopyable type causes:
|
Error message:
/usr/local/include/pybind11/pybind11.h:72:74: error: rvalue reference to type 'basic_string<...>' cannot bind to lvalue of type 'basic_string<...>'
initialize([f](Class *c, Arg... args) -> Return { return (c->*f)(args...); },
The text was updated successfully, but these errors were encountered: