index.rst 1.5 KB

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