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

Packit 534379
Type conversions
Packit 534379
################
Packit 534379
Packit 534379
Apart from enabling cross-language function calls, a fundamental problem
Packit 534379
that a binding tool like pybind11 must address is to provide access to
Packit 534379
native Python types in C++ and vice versa. There are three fundamentally
Packit 534379
different ways to do this—which approach is preferable for a particular type
Packit 534379
depends on the situation at hand.
Packit 534379
Packit 534379
1. Use a native C++ type everywhere. In this case, the type must be wrapped
Packit 534379
   using pybind11-generated bindings so that Python can interact with it.
Packit 534379
Packit 534379
2. Use a native Python type everywhere. It will need to be wrapped so that
Packit 534379
   C++ functions can interact with it.
Packit 534379
Packit 534379
3. Use a native C++ type on the C++ side and a native Python type on the
Packit 534379
   Python side. pybind11 refers to this as a *type conversion*.
Packit 534379
Packit 534379
   Type conversions are the most "natural" option in the sense that native
Packit 534379
   (non-wrapped) types are used everywhere. The main downside is that a copy
Packit 534379
   of the data must be made on every Python ↔ C++ transition: this is
Packit 534379
   needed since the C++ and Python versions of the same type generally won't
Packit 534379
   have the same memory layout.
Packit 534379
Packit 534379
   pybind11 can perform many kinds of conversions automatically. An overview
Packit 534379
   is provided in the table ":ref:`conversion_table`".
Packit 534379
Packit 534379
The following subsections discuss the differences between these options in more
Packit 534379
detail. The main focus in this section is on type conversions, which represent
Packit 534379
the last case of the above list.
Packit 534379
Packit 534379
.. toctree::
Packit 534379
   :maxdepth: 1
Packit 534379
Packit 534379
   overview
Packit 534379
   strings
Packit 534379
   stl
Packit 534379
   functional
Packit 534379
   chrono
Packit 534379
   eigen
Packit 534379
   custom
Packit 534379