Skip to content

def_buffer doesn't accept pointer-to-member-function #857

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

Closed
bmerry opened this issue May 17, 2017 · 0 comments
Closed

def_buffer doesn't accept pointer-to-member-function #857

bmerry opened this issue May 17, 2017 · 0 comments

Comments

@bmerry
Copy link
Contributor

bmerry commented May 17, 2017

Issue description

The implementation of def_buffer directly calls the passed function or function object, so a pointer-to-member-function causes compilation errors.

I'll provide a PR to fix it (I've got some code that should work, just need to add a regression test).

Reproducible example code

#include <pybind11/pybind11.h>
#include <cstdint>

namespace py = pybind11;

class A {
public:
    int32_t data = 0;

    py::buffer_info get_buffer_info()
    {
        return py::buffer_info(
            &data, sizeof(data),
            py::format_descriptor<decltype(data)>::format(),
            1);
    }
};

PYBIND11_PLUGIN(_buffer) {
    py::module m("_buffer");

    py::class_<A>(m, "A", py::buffer_protocol())
        .def(py::init<>())
        .def_readwrite("data", &A::data)
        .def_buffer(&A::get_buffer_info);

    return m.ptr();
}

Output (GCC 5.4):

    In file included from buffer.cpp:1:0:
    /home/bmerry/work/sdp/env/include/site/python2.7/pybind11/pybind11.h: In instantiation of ‘pybind11::class_<type_, options>::def_buffer(Func&&)::<lambda(PyObject*, void*)> [with Func = pybind11::buffer_info (A::*)(); type_ = A; options = {}; PyObject = _object]’:
    /home/bmerry/work/sdp/env/include/site/python2.7/pybind11/pybind11.h:1014:31:   required from ‘struct pybind11::class_<type_, options>::def_buffer(Func&&) [with Func = pybind11::buffer_info (A::*)(); type_ = A; options = {}]::<lambda(PyObject*, void*)>’
    /home/bmerry/work/sdp/env/include/site/python2.7/pybind11/pybind11.h:1014:9:   required from ‘pybind11::class_<type_, options>& pybind11::class_<type_, options>::def_buffer(Func&&) [with Func = pybind11::buffer_info (A::*)(); type_ = A; options = {}]’
    buffer.cpp:25:40:   required from here
    /home/bmerry/work/sdp/env/include/site/python2.7/pybind11/pybind11.h:1018:67: error: must use ‘.*’ or ‘->*’ to call pointer-to-member function in ‘((pybind11::class_<type_, options>::def_buffer(Func&&) [with Func = pybind11::buffer_info (A::*)(); type_ = A; options = {}]::capture*)ptr)->pybind11::class_<type_, options>::def_buffer(Func&&) [with Func = pybind11::buffer_info (A::*)(); type_ = A; options = {}]::capture::func (...)’, e.g. ‘(... ->* ((pybind11::class_<type_, options>::def_buffer(Func&&) [with Func = pybind11::buffer_info (A::*)(); type_ = A; options = {}]::capture*)ptr)->pybind11::class_<type_, options>::def_buffer(Func&&) [with Func = pybind11::buffer_info (A::*)(); type_ = A; options = {}]::capture::func) (...)’
                 return new buffer_info(((capture *) ptr)->func(caster));
bmerry added a commit to bmerry/pybind11 that referenced this issue May 17, 2017
Closes pybind#857. Instead of calling the function object directly, it wraps
it in std::bind and then immediately calls that. This is a poor-man's
form of std::invoke (not available in C++11 or C++14).
bmerry added a commit to bmerry/pybind11 that referenced this issue May 22, 2017
Closes pybind#857, by adding overloads to def_buffer that match pointers to
member functions and wrap them in lambdas.
bmerry added a commit to bmerry/pybind11 that referenced this issue May 22, 2017
Closes pybind#857, by adding overloads to def_buffer that match pointers to
member functions and wrap them in lambdas.
jagerman pushed a commit that referenced this issue May 22, 2017
Closes #857, by adding overloads to def_buffer that match pointers to
member functions and wrap them in lambdas.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant