Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
gaoqiong
pybind11
Commits
0991d7fb
Commit
0991d7fb
authored
Sep 05, 2017
by
Dean Moldovan
Browse files
Remove deprecated placement-new constructor from docs
[skip ci]
parent
a80af955
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
5 additions
and
37 deletions
+5
-37
docs/advanced/classes.rst
docs/advanced/classes.rst
+5
-37
No files found.
docs/advanced/classes.rst
View file @
0991d7fb
...
@@ -361,14 +361,8 @@ Custom constructors
...
@@ -361,14 +361,8 @@ Custom constructors
The syntax for binding constructors was previously introduced, but it only
The syntax for binding constructors was previously introduced, but it only
works when a constructor of the appropriate arguments actually exists on the
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
C++ side. To extend this to more general cases, pybind11 makes it possible
approaches: binding factory functions, and placement-new creation.
to bind factory functions as constructors. For example, suppose you have a
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
class like this:
class like this:
.. code-block:: cpp
.. code-block:: cpp
...
@@ -381,6 +375,9 @@ class like this:
...
@@ -381,6 +375,9 @@ class like this:
static Example create(int a) { return Example(a); }
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
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
``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(...))``
on the Python side. This can be accomplished by calling ``.def(py::init(...))``
...
@@ -463,35 +460,6 @@ an alias:
...
@@ -463,35 +460,6 @@ an alias:
.def(py::init([]() { return new PyExample(); }))
.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
Brace initialization
--------------------
--------------------
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment