Skip to content

Commit 436b731

Browse files
author
Wenzel Jakob
committed
added note about cast operations
1 parent 5cd3311 commit 436b731

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

docs/advanced.rst

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -711,6 +711,23 @@ Available types include :class:`handle`, :class:`object`, :class:`bool_`,
711711
:class:`dict`, :class:`slice`, :class:`capsule`, :class:`function`,
712712
:class:`buffer`, :class:`array`, and :class:`array_t`.
713713

714+
In this kind of mixed code, it is often necessary to convert arbitrary C++
715+
types to Python, which can be done using :func:`cast`:
716+
717+
.. code-block:: cpp
718+
719+
MyClass *cls = ..;
720+
py::object obj = py::cast(cls);
721+
722+
The reverse direction uses the following syntax:
723+
724+
.. code-block:: cpp
725+
726+
py::object obj = ...;
727+
MyClass *cls = obj.cast<MyClass *>();
728+
729+
When conversion fails, both directions throw the exception :class:`cast_error`.
730+
714731
.. seealso::
715732

716733
The file :file:`example/example2.cpp` contains a complete example that

0 commit comments

Comments
 (0)