Skip to content

Problem with constructor for a class with custom operator new #948

Description

@dimar

Hello, all!

I have a problem exposing a class with custom operator new (custom allocation in a heap book).
Here is an example:

#include<pybind11/pybind11.h>namespacepy= pybind11;
classExample {
public:staticvoid* operatornew(std::size_t size);
staticvoidoperatordelete(void* p, std::size_t size);
Example() {}
};
PYBIND11_MODULE(test, m) {
m.doc() = "test";
py::class_<Example>(m, "Example")
.def(py::init<>())
;
}

The problem arises due to the fact that this statement

 .def(py::init<>())

is a shorthand for

.def("__init__", [](Example &instance) {new (&instance) Example();}

which results in the next error:
pybind11/pybind11.h:1320:60: error: no matching function for call to ‘Example::operator new(sizetype, Base*&)’ cl.def("__init__", [](Base *self_, Args... args) { new (self_) Base(args...); }, extra...);

It worked in Boost.Python out-of-the-box, because they have, apparently, different way of creating a constructor's bindings.

How it is possible to resolve this problem?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions