From 436b7318912d2718ce1df5753556ddd7dc8414ab Mon Sep 17 00:00:00 2001 From: Wenzel Jakob Date: Tue, 20 Oct 2015 01:04:30 +0200 Subject: [PATCH] added note about cast operations --- docs/advanced.rst | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/docs/advanced.rst b/docs/advanced.rst index 4caacf9c..cf6dd8a6 100644 --- a/docs/advanced.rst +++ b/docs/advanced.rst @@ -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(); + +When conversion fails, both directions throw the exception :class:`cast_error`. + .. seealso:: The file :file:`example/example2.cpp` contains a complete example that