diff --git a/docs/advanced/classes.rst b/docs/advanced/classes.rst index c0a9cfa5f..93deeec62 100644 --- a/docs/advanced/classes.rst +++ b/docs/advanced/classes.rst @@ -361,14 +361,8 @@ Custom constructors The syntax for binding constructors was previously introduced, but it only works when a constructor of the appropriate arguments actually exists on the -C++ side. To extend this to more general cases, pybind11 offers two different -approaches: binding factory functions, and placement-new creation. - -Factory function constructors ------------------------------ - -It is possible to expose a Python-side constructor from a C++ function that -returns a new object by value or pointer. For example, suppose you have a +C++ side. To extend this to more general cases, pybind11 makes it possible +to bind factory functions as constructors. For example, suppose you have a class like this: .. code-block:: cpp @@ -381,6 +375,9 @@ class like this: static Example create(int a) { return Example(a); } }; + py::class_(m, "Example") + .def(py::init(&Example::create)); + While it is possible to create a straightforward binding of the static ``create`` method, it may sometimes be preferable to expose it as a constructor on the Python side. This can be accomplished by calling ``.def(py::init(...))`` @@ -463,35 +460,6 @@ an alias: .def(py::init([]() { return new PyExample(); })) ; -Low-level placement-new construction ------------------------------------- - -A second approach for creating new instances use C++ placement new to construct -an object in-place in preallocated memory. To do this, you simply bind a -method name ``__init__`` that takes the class instance as the first argument by -pointer or reference, then uses a placement-new constructor to construct the -object in the pre-allocated (but uninitialized) memory. - -For example, instead of: - -.. code-block:: cpp - - py::class_(m, "Example") - .def(py::init()); - -you could equivalently write: - -.. code-block:: cpp - - py::class_(m, "Example") - .def("__init__", - [](Example &instance, int arg) { - new (&instance) Example(arg); - } - ); - -which will invoke the constructor in-place at the pre-allocated memory. - Brace initialization --------------------