blob: a0e4d5666b5a86a246e35cd62b749ab5fa804a8d [file] [log] [blame]
Dean Moldovanf0b0df52016-10-19 22:13:27 +02001Type conversions
2################
3
4There are 3 mechanisms that pybind11 uses to move data between C++ and Python.
5We'll take a quick look at each one to get an overview of what's happening.
6
7.. rubric:: 1. Native type in C++, wrapper in Python
8
9Exposing a custom C++ type using :class:`py::class_` was covered in detail in
10the :doc:`/classes` section. There, the underlying data structure is always the
11original C++ class while the :class:`py::class_` wrapper provides a Python
12interface. Internally, when an object like this is sent from C++ to Python,
13pybind11 will just add the outer wrapper layer over the native C++ object.
14Getting it back from Python is just a matter of peeling off the wrapper.
15
16.. rubric:: 2. Wrapper in C++, native type in Python
17
18This is the exact opposite situation. Now, we have a type which is native to
19Python, like a ``tuple`` or a ``list``. One way to get this data into C++ is
20with the :class:`py::object` family of wrappers. These are explained in more
21detail in the :doc:`/advanced/pycpp/object` section. We'll just give a quick
22example here:
23
24.. code-block:: cpp
25
26 void print_list(py::list my_list) {
27 for (auto item : my_list)
28 std::cout << item << " ";
29 }
30
31.. code-block:: pycon
32
33 >>> print_list([1, 2, 3])
34 1 2 3
35
36The Python ``list`` is not converted in any way -- it's just wrapped in a C++
37:class:`py::list` class. At its core it's still a Python object. Copying a
38:class:`py::list` will do the usual reference-counting like in Python.
39Returning the object to Python will just remove the thin wrapper.
40
41.. rubric:: 3. Converting between native C++ and Python types
42
43In the previous two cases we had a native type in one language and a wrapper in
44the other. Now, we have native types on both sides and we convert between them.
45
46.. code-block:: cpp
47
48 void print_vector(const std::vector<int> &v) {
49 for (auto item : v)
50 std::cout << item << "\n";
51 }
52
53.. code-block:: pycon
54
55 >>> print_vector([1, 2, 3])
56 1 2 3
57
58In this case, pybind11 will construct a new ``std::vector<int>`` and copy each
59element from the Python ``list``. The newly constructed object will be passed
60to ``print_vector``. The same thing happens in the other direction: a new
61``list`` is made to match the value returned from C++.
62
63Lots of these conversions are supported out of the box, as shown in the table
64below. They are very convenient, but keep in mind that these conversions are
65fundamentally based on copying data. This is perfectly fine for small immutable
66types but it may become quite expensive for large data structures. This can be
67avoided by overriding the automatic conversion with a custom wrapper (i.e. the
68above-mentioned approach 1). This requires some manual effort and more details
69are available in the :ref:`opaque` section.
70
71.. rubric:: Supported automatic conversions
Dean Moldovan67b52d82016-10-16 19:12:43 +020072
73.. toctree::
74 :maxdepth: 1
75
76 stl
77 functional
78 chrono
79 eigen
80 custom
81
82The following basic data types are supported out of the box (some may require
83an additional extension header to be included). To pass other data structures
84as arguments and return values, refer to the section on binding :ref:`classes`.
85
86+---------------------------------+--------------------------+-------------------------------+
87| Data type | Description | Header file |
88+=================================+==========================+===============================+
89| ``int8_t``, ``uint8_t`` | 8-bit integers | :file:`pybind11/pybind11.h` |
90+---------------------------------+--------------------------+-------------------------------+
91| ``int16_t``, ``uint16_t`` | 16-bit integers | :file:`pybind11/pybind11.h` |
92+---------------------------------+--------------------------+-------------------------------+
93| ``int32_t``, ``uint32_t`` | 32-bit integers | :file:`pybind11/pybind11.h` |
94+---------------------------------+--------------------------+-------------------------------+
95| ``int64_t``, ``uint64_t`` | 64-bit integers | :file:`pybind11/pybind11.h` |
96+---------------------------------+--------------------------+-------------------------------+
97| ``ssize_t``, ``size_t`` | Platform-dependent size | :file:`pybind11/pybind11.h` |
98+---------------------------------+--------------------------+-------------------------------+
99| ``float``, ``double`` | Floating point types | :file:`pybind11/pybind11.h` |
100+---------------------------------+--------------------------+-------------------------------+
101| ``bool`` | Two-state Boolean type | :file:`pybind11/pybind11.h` |
102+---------------------------------+--------------------------+-------------------------------+
103| ``char`` | Character literal | :file:`pybind11/pybind11.h` |
104+---------------------------------+--------------------------+-------------------------------+
105| ``wchar_t`` | Wide character literal | :file:`pybind11/pybind11.h` |
106+---------------------------------+--------------------------+-------------------------------+
107| ``const char *`` | UTF-8 string literal | :file:`pybind11/pybind11.h` |
108+---------------------------------+--------------------------+-------------------------------+
109| ``const wchar_t *`` | Wide string literal | :file:`pybind11/pybind11.h` |
110+---------------------------------+--------------------------+-------------------------------+
111| ``std::string`` | STL dynamic UTF-8 string | :file:`pybind11/pybind11.h` |
112+---------------------------------+--------------------------+-------------------------------+
113| ``std::wstring`` | STL dynamic wide string | :file:`pybind11/pybind11.h` |
114+---------------------------------+--------------------------+-------------------------------+
115| ``std::pair<T1, T2>`` | Pair of two custom types | :file:`pybind11/pybind11.h` |
116+---------------------------------+--------------------------+-------------------------------+
117| ``std::tuple<...>`` | Arbitrary tuple of types | :file:`pybind11/pybind11.h` |
118+---------------------------------+--------------------------+-------------------------------+
119| ``std::reference_wrapper<...>`` | Reference type wrapper | :file:`pybind11/pybind11.h` |
120+---------------------------------+--------------------------+-------------------------------+
121| ``std::complex<T>`` | Complex numbers | :file:`pybind11/complex.h` |
122+---------------------------------+--------------------------+-------------------------------+
123| ``std::array<T, Size>`` | STL static array | :file:`pybind11/stl.h` |
124+---------------------------------+--------------------------+-------------------------------+
125| ``std::vector<T>`` | STL dynamic array | :file:`pybind11/stl.h` |
126+---------------------------------+--------------------------+-------------------------------+
127| ``std::list<T>`` | STL linked list | :file:`pybind11/stl.h` |
128+---------------------------------+--------------------------+-------------------------------+
129| ``std::map<T1, T2>`` | STL ordered map | :file:`pybind11/stl.h` |
130+---------------------------------+--------------------------+-------------------------------+
131| ``std::unordered_map<T1, T2>`` | STL unordered map | :file:`pybind11/stl.h` |
132+---------------------------------+--------------------------+-------------------------------+
133| ``std::set<T>`` | STL ordered set | :file:`pybind11/stl.h` |
134+---------------------------------+--------------------------+-------------------------------+
135| ``std::unordered_set<T>`` | STL unordered set | :file:`pybind11/stl.h` |
136+---------------------------------+--------------------------+-------------------------------+
137| ``std::function<...>`` | STL polymorphic function | :file:`pybind11/functional.h` |
138+---------------------------------+--------------------------+-------------------------------+
139| ``std::chrono::duration<...>`` | STL time duration | :file:`pybind11/chrono.h` |
140+---------------------------------+--------------------------+-------------------------------+
141| ``std::chrono::time_point<...>``| STL date/time | :file:`pybind11/chrono.h` |
142+---------------------------------+--------------------------+-------------------------------+
143| ``Eigen::Matrix<...>`` | Eigen: dense matrix | :file:`pybind11/eigen.h` |
144+---------------------------------+--------------------------+-------------------------------+
145| ``Eigen::Map<...>`` | Eigen: mapped memory | :file:`pybind11/eigen.h` |
146+---------------------------------+--------------------------+-------------------------------+
147| ``Eigen::SparseMatrix<...>`` | Eigen: sparse matrix | :file:`pybind11/eigen.h` |
148+---------------------------------+--------------------------+-------------------------------+