Skip to content

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

@dimar

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>
namespace py = pybind11;

class Example {
public:
    static void*    operator new(std::size_t size);
    static void     operator delete(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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions