mirror of
https://github.com/pybind/pybind11.git
synced 2024-11-21 20:55:11 +00:00
Remove deprecated placement-new constructor from docs
[skip ci]
This commit is contained in:
parent
a80af9557d
commit
0991d7fba1
@ -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_<Example>(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_<Example>(m, "Example")
|
||||
.def(py::init<int>());
|
||||
|
||||
you could equivalently write:
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
py::class_<Example>(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
|
||||
--------------------
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user