Blame external/pybind11/docs/advanced/cast/overview.rst

Packit 534379
Overview
Packit 534379
########
Packit 534379
Packit 534379
.. rubric:: 1. Native type in C++, wrapper in Python
Packit 534379
Packit 534379
Exposing a custom C++ type using :class:`py::class_` was covered in detail
Packit 534379
in the :doc:`/classes` section. There, the underlying data structure is
Packit 534379
always the original C++ class while the :class:`py::class_` wrapper provides
Packit 534379
a Python interface. Internally, when an object like this is sent from C++ to
Packit 534379
Python, pybind11 will just add the outer wrapper layer over the native C++
Packit 534379
object. Getting it back from Python is just a matter of peeling off the
Packit 534379
wrapper.
Packit 534379
Packit 534379
.. rubric:: 2. Wrapper in C++, native type in Python
Packit 534379
Packit 534379
This is the exact opposite situation. Now, we have a type which is native to
Packit 534379
Python, like a ``tuple`` or a ``list``. One way to get this data into C++ is
Packit 534379
with the :class:`py::object` family of wrappers. These are explained in more
Packit 534379
detail in the :doc:`/advanced/pycpp/object` section. We'll just give a quick
Packit 534379
example here:
Packit 534379
Packit 534379
.. code-block:: cpp
Packit 534379
Packit 534379
    void print_list(py::list my_list) {
Packit 534379
        for (auto item : my_list)
Packit 534379
            std::cout << item << " ";
Packit 534379
    }
Packit 534379
Packit 534379
.. code-block:: pycon
Packit 534379
Packit 534379
    >>> print_list([1, 2, 3])
Packit 534379
    1 2 3
Packit 534379
Packit 534379
The Python ``list`` is not converted in any way -- it's just wrapped in a C++
Packit 534379
:class:`py::list` class. At its core it's still a Python object. Copying a
Packit 534379
:class:`py::list` will do the usual reference-counting like in Python.
Packit 534379
Returning the object to Python will just remove the thin wrapper.
Packit 534379
Packit 534379
.. rubric:: 3. Converting between native C++ and Python types
Packit 534379
Packit 534379
In the previous two cases we had a native type in one language and a wrapper in
Packit 534379
the other. Now, we have native types on both sides and we convert between them.
Packit 534379
Packit 534379
.. code-block:: cpp
Packit 534379
Packit 534379
    void print_vector(const std::vector<int> &v) {
Packit 534379
        for (auto item : v)
Packit 534379
            std::cout << item << "\n";
Packit 534379
    }
Packit 534379
Packit 534379
.. code-block:: pycon
Packit 534379
Packit 534379
    >>> print_vector([1, 2, 3])
Packit 534379
    1 2 3
Packit 534379
Packit 534379
In this case, pybind11 will construct a new ``std::vector<int>`` and copy each
Packit 534379
element from the Python ``list``. The newly constructed object will be passed
Packit 534379
to ``print_vector``. The same thing happens in the other direction: a new
Packit 534379
``list`` is made to match the value returned from C++.
Packit 534379
Packit 534379
Lots of these conversions are supported out of the box, as shown in the table
Packit 534379
below. They are very convenient, but keep in mind that these conversions are
Packit 534379
fundamentally based on copying data. This is perfectly fine for small immutable
Packit 534379
types but it may become quite expensive for large data structures. This can be
Packit 534379
avoided by overriding the automatic conversion with a custom wrapper (i.e. the
Packit 534379
above-mentioned approach 1). This requires some manual effort and more details
Packit 534379
are available in the :ref:`opaque` section.
Packit 534379
Packit 534379
.. _conversion_table:
Packit 534379
Packit 534379
List of all builtin conversions
Packit 534379
-------------------------------
Packit 534379
Packit 534379
The following basic data types are supported out of the box (some may require
Packit 534379
an additional extension header to be included). To pass other data structures
Packit 534379
as arguments and return values, refer to the section on binding :ref:`classes`.
Packit 534379
Packit 534379
+------------------------------------+---------------------------+-------------------------------+
Packit 534379
|  Data type                         |  Description              | Header file                   |
Packit 534379
+====================================+===========================+===============================+
Packit 534379
| ``int8_t``, ``uint8_t``            | 8-bit integers            | :file:`pybind11/pybind11.h`   |
Packit 534379
+------------------------------------+---------------------------+-------------------------------+
Packit 534379
| ``int16_t``, ``uint16_t``          | 16-bit integers           | :file:`pybind11/pybind11.h`   |
Packit 534379
+------------------------------------+---------------------------+-------------------------------+
Packit 534379
| ``int32_t``, ``uint32_t``          | 32-bit integers           | :file:`pybind11/pybind11.h`   |
Packit 534379
+------------------------------------+---------------------------+-------------------------------+
Packit 534379
| ``int64_t``, ``uint64_t``          | 64-bit integers           | :file:`pybind11/pybind11.h`   |
Packit 534379
+------------------------------------+---------------------------+-------------------------------+
Packit 534379
| ``ssize_t``, ``size_t``            | Platform-dependent size   | :file:`pybind11/pybind11.h`   |
Packit 534379
+------------------------------------+---------------------------+-------------------------------+
Packit 534379
| ``float``, ``double``              | Floating point types      | :file:`pybind11/pybind11.h`   |
Packit 534379
+------------------------------------+---------------------------+-------------------------------+
Packit 534379
| ``bool``                           | Two-state Boolean type    | :file:`pybind11/pybind11.h`   |
Packit 534379
+------------------------------------+---------------------------+-------------------------------+
Packit 534379
| ``char``                           | Character literal         | :file:`pybind11/pybind11.h`   |
Packit 534379
+------------------------------------+---------------------------+-------------------------------+
Packit 534379
| ``char16_t``                       | UTF-16 character literal  | :file:`pybind11/pybind11.h`   |
Packit 534379
+------------------------------------+---------------------------+-------------------------------+
Packit 534379
| ``char32_t``                       | UTF-32 character literal  | :file:`pybind11/pybind11.h`   |
Packit 534379
+------------------------------------+---------------------------+-------------------------------+
Packit 534379
| ``wchar_t``                        | Wide character literal    | :file:`pybind11/pybind11.h`   |
Packit 534379
+------------------------------------+---------------------------+-------------------------------+
Packit 534379
| ``const char *``                   | UTF-8 string literal      | :file:`pybind11/pybind11.h`   |
Packit 534379
+------------------------------------+---------------------------+-------------------------------+
Packit 534379
| ``const char16_t *``               | UTF-16 string literal     | :file:`pybind11/pybind11.h`   |
Packit 534379
+------------------------------------+---------------------------+-------------------------------+
Packit 534379
| ``const char32_t *``               | UTF-32 string literal     | :file:`pybind11/pybind11.h`   |
Packit 534379
+------------------------------------+---------------------------+-------------------------------+
Packit 534379
| ``const wchar_t *``                | Wide string literal       | :file:`pybind11/pybind11.h`   |
Packit 534379
+------------------------------------+---------------------------+-------------------------------+
Packit 534379
| ``std::string``                    | STL dynamic UTF-8 string  | :file:`pybind11/pybind11.h`   |
Packit 534379
+------------------------------------+---------------------------+-------------------------------+
Packit 534379
| ``std::u16string``                 | STL dynamic UTF-16 string | :file:`pybind11/pybind11.h`   |
Packit 534379
+------------------------------------+---------------------------+-------------------------------+
Packit 534379
| ``std::u32string``                 | STL dynamic UTF-32 string | :file:`pybind11/pybind11.h`   |
Packit 534379
+------------------------------------+---------------------------+-------------------------------+
Packit 534379
| ``std::wstring``                   | STL dynamic wide string   | :file:`pybind11/pybind11.h`   |
Packit 534379
+------------------------------------+---------------------------+-------------------------------+
Packit 534379
| ``std::string_view``,              | STL C++17 string views    | :file:`pybind11/pybind11.h`   |
Packit 534379
| ``std::u16string_view``, etc.      |                           |                               |
Packit 534379
+------------------------------------+---------------------------+-------------------------------+
Packit 534379
| ``std::pair<T1, T2>``              | Pair of two custom types  | :file:`pybind11/pybind11.h`   |
Packit 534379
+------------------------------------+---------------------------+-------------------------------+
Packit 534379
| ``std::tuple<...>``                | Arbitrary tuple of types  | :file:`pybind11/pybind11.h`   |
Packit 534379
+------------------------------------+---------------------------+-------------------------------+
Packit 534379
| ``std::reference_wrapper<...>``    | Reference type wrapper    | :file:`pybind11/pybind11.h`   |
Packit 534379
+------------------------------------+---------------------------+-------------------------------+
Packit 534379
| ``std::complex<T>``                | Complex numbers           | :file:`pybind11/complex.h`    |
Packit 534379
+------------------------------------+---------------------------+-------------------------------+
Packit 534379
| ``std::array<T, Size>``            | STL static array          | :file:`pybind11/stl.h`        |
Packit 534379
+------------------------------------+---------------------------+-------------------------------+
Packit 534379
| ``std::vector<T>``                 | STL dynamic array         | :file:`pybind11/stl.h`        |
Packit 534379
+------------------------------------+---------------------------+-------------------------------+
Packit 534379
| ``std::deque<T>``                  | STL double-ended queue    | :file:`pybind11/stl.h`        |
Packit 534379
+------------------------------------+---------------------------+-------------------------------+
Packit 534379
| ``std::valarray<T>``               | STL value array           | :file:`pybind11/stl.h`        |
Packit 534379
+------------------------------------+---------------------------+-------------------------------+
Packit 534379
| ``std::list<T>``                   | STL linked list           | :file:`pybind11/stl.h`        |
Packit 534379
+------------------------------------+---------------------------+-------------------------------+
Packit 534379
| ``std::map<T1, T2>``               | STL ordered map           | :file:`pybind11/stl.h`        |
Packit 534379
+------------------------------------+---------------------------+-------------------------------+
Packit 534379
| ``std::unordered_map<T1, T2>``     | STL unordered map         | :file:`pybind11/stl.h`        |
Packit 534379
+------------------------------------+---------------------------+-------------------------------+
Packit 534379
| ``std::set<T>``                    | STL ordered set           | :file:`pybind11/stl.h`        |
Packit 534379
+------------------------------------+---------------------------+-------------------------------+
Packit 534379
| ``std::unordered_set<T>``          | STL unordered set         | :file:`pybind11/stl.h`        |
Packit 534379
+------------------------------------+---------------------------+-------------------------------+
Packit 534379
| ``std::optional<T>``               | STL optional type (C++17) | :file:`pybind11/stl.h`        |
Packit 534379
+------------------------------------+---------------------------+-------------------------------+
Packit 534379
| ``std::experimental::optional<T>`` | STL optional type (exp.)  | :file:`pybind11/stl.h`        |
Packit 534379
+------------------------------------+---------------------------+-------------------------------+
Packit 534379
| ``std::variant<...>``              | Type-safe union (C++17)   | :file:`pybind11/stl.h`        |
Packit 534379
+------------------------------------+---------------------------+-------------------------------+
Packit 534379
| ``std::function<...>``             | STL polymorphic function  | :file:`pybind11/functional.h` |
Packit 534379
+------------------------------------+---------------------------+-------------------------------+
Packit 534379
| ``std::chrono::duration<...>``     | STL time duration         | :file:`pybind11/chrono.h`     |
Packit 534379
+------------------------------------+---------------------------+-------------------------------+
Packit 534379
| ``std::chrono::time_point<...>``   | STL date/time             | :file:`pybind11/chrono.h`     |
Packit 534379
+------------------------------------+---------------------------+-------------------------------+
Packit 534379
| ``Eigen::Matrix<...>``             | Eigen: dense matrix       | :file:`pybind11/eigen.h`      |
Packit 534379
+------------------------------------+---------------------------+-------------------------------+
Packit 534379
| ``Eigen::Map<...>``                | Eigen: mapped memory      | :file:`pybind11/eigen.h`      |
Packit 534379
+------------------------------------+---------------------------+-------------------------------+
Packit 534379
| ``Eigen::SparseMatrix<...>``       | Eigen: sparse matrix      | :file:`pybind11/eigen.h`      |
Packit 534379
+------------------------------------+---------------------------+-------------------------------+