Skip to content

Commit

Permalink
document a caveat about in-place operators
Browse files Browse the repository at this point in the history
  • Loading branch information
wjakob committed Mar 2, 2024
1 parent 08b8710 commit 0316867
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions docs/classes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -716,6 +716,17 @@ C++ side, or to perform other types of customization. The
nanobind that this is an operator, which returns ``NotImplemented`` when
invoked with incompatible arguments rather than throwing a type error.

When binding *in-place* operators such as ``operator+=``, and when their
implementation is guaranteed to end with ``return *this``, it is recommended
that you set a return value policy of :cpp:enumerator:`rv_policy::none`, i.e.,

.. code-block:: cpp
.def(nb::self += nb::self, nb::rv_policy::none)
Otherwise, the function binding will return a new copy of the object, which is
usually not desired.

Binding protected member functions
----------------------------------

Expand Down

0 comments on commit 0316867

Please sign in to comment.