Skip to content

Commit 7830e85

Browse files
jbarlow83wjakob
authored andcommitted
Docs: minor clarifications (#590)
* Some clarifications to section on virtual fns Primarily, I made it clear that PYBIND11_OVERLOAD_PURE_NAME is not "useful" but required in renaming situations. Also clarified that one should not bind to the trampoline helper class which I found tempting since it seems more explicit. * Remove :emphasize-lines: from cpp block, seems to suppress formatting * docs: emphasize default policy, clarify keep_alive Emphasize the default return value policy since this statement is hidden in a wall of text. Add a hint that call policies are probably required for container objects.
1 parent 9b815ad commit 7830e85

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

docs/advanced/classes.rst

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ helper class that is defined as follows:
7979
PYBIND11_OVERLOAD_PURE(
8080
std::string, /* Return type */
8181
Animal, /* Parent class */
82-
go, /* Name of function */
82+
go, /* Name of function in C++ (must match Python name) */
8383
n_times /* Argument(s) */
8484
);
8585
}
@@ -90,7 +90,8 @@ functions, and :func:`PYBIND11_OVERLOAD` should be used for functions which have
9090
a default implementation. There are also two alternate macros
9191
:func:`PYBIND11_OVERLOAD_PURE_NAME` and :func:`PYBIND11_OVERLOAD_NAME` which
9292
take a string-valued name argument between the *Parent class* and *Name of the
93-
function* slots. This is useful when the C++ and Python versions of the
93+
function* slots, which defines the name of function in Python. This is required
94+
when the C++ and Python versions of the
9495
function have different names, e.g. ``operator()`` vs ``__call__``.
9596

9697
The binding code also needs a few minor adaptations (highlighted):
@@ -115,11 +116,20 @@ The binding code also needs a few minor adaptations (highlighted):
115116
}
116117
117118
Importantly, pybind11 is made aware of the trampoline helper class by
118-
specifying it as an extra template argument to :class:`class_`. (This can also
119+
specifying it as an extra template argument to :class:`class_`. (This can also
119120
be combined with other template arguments such as a custom holder type; the
120121
order of template types does not matter). Following this, we are able to
121122
define a constructor as usual.
122123

124+
Bindings should be made against the actual class, not the trampoline helper class.
125+
126+
.. code-block:: cpp
127+
128+
py::class_<Animal, PyAnimal /* <--- trampoline*/> animal(m, "Animal");
129+
animal
130+
.def(py::init<>())
131+
.def("go", &PyAnimal::go); /* <--- THIS IS WRONG, use &Animal::go */
132+
123133
Note, however, that the above is sufficient for allowing python classes to
124134
extend ``Animal``, but not ``Dog``: see ref:`virtual_and_inheritance` for the
125135
necessary steps required to providing proper overload support for inherited

docs/advanced/functions.rst

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ The following table provides an overview of available policies:
8888
| | return value is referenced by Python. This is the default policy for |
8989
| | property getters created via ``def_property``, ``def_readwrite``, etc. |
9090
+--------------------------------------------------+----------------------------------------------------------------------------+
91-
| :enum:`return_value_policy::automatic` | This is the default return value policy, which falls back to the policy |
91+
| :enum:`return_value_policy::automatic` | **Default policy.** This policy falls back to the policy |
9292
| | :enum:`return_value_policy::take_ownership` when the return value is a |
9393
| | pointer. Otherwise, it uses :enum:`return_value::move` or |
9494
| | :enum:`return_value::copy` for rvalue and lvalue references, respectively. |
@@ -159,7 +159,11 @@ Additional call policies
159159
========================
160160

161161
In addition to the above return value policies, further `call policies` can be
162-
specified to indicate dependencies between parameters. There is currently just
162+
specified to indicate dependencies between parameters. In general, call policies
163+
are required when the C++ object is any kind of container and another object is being
164+
added to the container.
165+
166+
There is currently just
163167
one policy named ``keep_alive<Nurse, Patient>``, which indicates that the
164168
argument with index ``Patient`` should be kept alive at least until the
165169
argument with index ``Nurse`` is freed by the garbage collector. Argument

0 commit comments

Comments
 (0)