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
5ef12190
Commit
5ef12190
authored
Dec 15, 2015
by
Wenzel Jakob
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
smart pointer clarifications
parent
8b5bf00f
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
25 additions
and
8 deletions
+25
-8
docs/advanced.rst
+25
-8
No files found.
docs/advanced.rst
View file @
5ef12190
...
@@ -494,10 +494,16 @@ following snippet causes ``std::shared_ptr`` to be used instead.
...
@@ -494,10 +494,16 @@ following snippet causes ``std::shared_ptr`` to be used instead.
.. code-block:: cpp
.. code-block:: cpp
py::class_<Example, std::shared_ptr<Example>> obj(m, "Example");
/// Type declaration
class Example : public std::enable_shared_from_this<Example> /* <- important, see below */ {
// ...
};
/// .... code within PYBIND11_PLUGIN declaration .....
py::class_<Example, std::shared_ptr<Example> /* <- important */> obj(m, "Example");
To enable transparent conversions for functions that take shared pointers as an
To enable transparent conversions for functions that take shared pointers as an
argument or that return them, a macro invocation similar to the following must
argument or that return them, a macro invocation similar to the following must
be declared at the top level before any binding code:
be declared at the top level before any binding code:
.. code-block:: cpp
.. code-block:: cpp
...
@@ -512,20 +518,31 @@ be declared at the top level before any binding code:
...
@@ -512,20 +518,31 @@ be declared at the top level before any binding code:
both sides; also, don't use the name of a type that already exists in your
both sides; also, don't use the name of a type that already exists in your
codebase.
codebase.
.. seealso::
The file :file:`example/example8.cpp` contains a complete example that
demonstrates how to work with custom reference-counting holder types in
more detail.
.. warning::
.. warning::
To ensure correct reference counting among Python and C++, the use of
To ensure correct reference counting among Python and C++, the use of
``std::shared_ptr<T>`` as a holder type requires that ``T`` inherits from
``std::shared_ptr<T>`` as a holder type requires that ``T`` inherits from
``std::enable_shared_from_this<T>`` (see cppreference_ for details).
``std::enable_shared_from_this<T>`` (see cppreference_ for details).
If you encounter issues (failure to compile, ``bad_weak_ptr`` exceptions),
please check that you really did all three steps:
1. invoking the ``PYBIND11_DECLARE_HOLDER_TYPE`` macro in every file that
contains pybind11 code and uses your chosen smart pointer type.
2. specifying the holder types to ``class_``.
3. extending from ``std::enable_shared_from_this`` when using
``std::shared_ptr``.
.. _cppreference: http://en.cppreference.com/w/cpp/memory/enable_shared_from_this
.. _cppreference: http://en.cppreference.com/w/cpp/memory/enable_shared_from_this
.. seealso::
The file :file:`example/example8.cpp` contains a complete example that
demonstrates how to work with custom reference-counting holder types in
more detail.
.. _custom_constructors:
.. _custom_constructors:
Custom constructors
Custom constructors
...
...
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