Skip to content

Commit

Permalink
added note about cast operations
Browse files Browse the repository at this point in the history
  • Loading branch information
Wenzel Jakob committed Oct 19, 2015
1 parent 5cd3311 commit 436b731
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions docs/advanced.rst
Original file line number Diff line number Diff line change
Expand Up @@ -711,6 +711,23 @@ Available types include :class:`handle`, :class:`object`, :class:`bool_`,
:class:`dict`, :class:`slice`, :class:`capsule`, :class:`function`,
:class:`buffer`, :class:`array`, and :class:`array_t`.

In this kind of mixed code, it is often necessary to convert arbitrary C++
types to Python, which can be done using :func:`cast`:

.. code-block:: cpp
MyClass *cls = ..;
py::object obj = py::cast(cls);
The reverse direction uses the following syntax:

.. code-block:: cpp
py::object obj = ...;
MyClass *cls = obj.cast<MyClass *>();
When conversion fails, both directions throw the exception :class:`cast_error`.

.. seealso::

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

0 comments on commit 436b731

Please sign in to comment.