overview.rst 11 KB

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