Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
P
pybind11
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
open
pybind11
Commits
0991d7fb
Commit
0991d7fb
authored
Sep 05, 2017
by
Dean Moldovan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Remove deprecated placement-new constructor from docs
[skip ci]
parent
a80af955
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
5 additions
and
37 deletions
+5
-37
docs/advanced/classes.rst
+5
-37
No files found.
docs/advanced/classes.rst
View file @
0991d7fb
...
...
@@ -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
--------------------
...
...
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