• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1.. _type-conversions:
2
3Type conversions
4################
5
6Apart from enabling cross-language function calls, a fundamental problem
7that a binding tool like pybind11 must address is to provide access to
8native Python types in C++ and vice versa. There are three fundamentally
9different ways to do this—which approach is preferable for a particular type
10depends on the situation at hand.
11
121. Use a native C++ type everywhere. In this case, the type must be wrapped
13   using pybind11-generated bindings so that Python can interact with it.
14
152. Use a native Python type everywhere. It will need to be wrapped so that
16   C++ functions can interact with it.
17
183. Use a native C++ type on the C++ side and a native Python type on the
19   Python side. pybind11 refers to this as a *type conversion*.
20
21   Type conversions are the most "natural" option in the sense that native
22   (non-wrapped) types are used everywhere. The main downside is that a copy
23   of the data must be made on every Python ↔ C++ transition: this is
24   needed since the C++ and Python versions of the same type generally won't
25   have the same memory layout.
26
27   pybind11 can perform many kinds of conversions automatically. An overview
28   is provided in the table ":ref:`conversion_table`".
29
30The following subsections discuss the differences between these options in more
31detail. The main focus in this section is on type conversions, which represent
32the last case of the above list.
33
34.. toctree::
35   :maxdepth: 1
36
37   overview
38   strings
39   stl
40   functional
41   chrono
42   eigen
43   custom
44