Henry Schreiner | f12ec00 | 2020-09-14 18:06:26 -0400 | [diff] [blame^] | 1 | .. _type-conversions: |
| 2 | |
Dean Moldovan | f0b0df5 | 2016-10-19 22:13:27 +0200 | [diff] [blame] | 3 | Type conversions |
| 4 | ################ |
| 5 | |
Wenzel Jakob | 6ba9865 | 2016-10-24 23:48:20 +0200 | [diff] [blame] | 6 | Apart from enabling cross-language function calls, a fundamental problem |
| 7 | that a binding tool like pybind11 must address is to provide access to |
| 8 | native Python types in C++ and vice versa. There are three fundamentally |
| 9 | different ways to do this—which approach is preferable for a particular type |
| 10 | depends on the situation at hand. |
Dean Moldovan | f0b0df5 | 2016-10-19 22:13:27 +0200 | [diff] [blame] | 11 | |
Wenzel Jakob | 6ba9865 | 2016-10-24 23:48:20 +0200 | [diff] [blame] | 12 | 1. 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. |
Dean Moldovan | f0b0df5 | 2016-10-19 22:13:27 +0200 | [diff] [blame] | 14 | |
Wenzel Jakob | 6ba9865 | 2016-10-24 23:48:20 +0200 | [diff] [blame] | 15 | 2. Use a native Python type everywhere. It will need to be wrapped so that |
| 16 | C++ functions can interact with it. |
Dean Moldovan | f0b0df5 | 2016-10-19 22:13:27 +0200 | [diff] [blame] | 17 | |
Wenzel Jakob | 6ba9865 | 2016-10-24 23:48:20 +0200 | [diff] [blame] | 18 | 3. 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*. |
Dean Moldovan | f0b0df5 | 2016-10-19 22:13:27 +0200 | [diff] [blame] | 20 | |
Wenzel Jakob | 6ba9865 | 2016-10-24 23:48:20 +0200 | [diff] [blame] | 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. |
Dean Moldovan | f0b0df5 | 2016-10-19 22:13:27 +0200 | [diff] [blame] | 26 | |
Wenzel Jakob | 6ba9865 | 2016-10-24 23:48:20 +0200 | [diff] [blame] | 27 | pybind11 can perform many kinds of conversions automatically. An overview |
| 28 | is provided in the table ":ref:`conversion_table`". |
Dean Moldovan | f0b0df5 | 2016-10-19 22:13:27 +0200 | [diff] [blame] | 29 | |
Wenzel Jakob | 6ba9865 | 2016-10-24 23:48:20 +0200 | [diff] [blame] | 30 | The following subsections discuss the differences between these options in more |
| 31 | detail. The main focus in this section is on type conversions, which represent |
| 32 | the last case of the above list. |
Dean Moldovan | 67b52d8 | 2016-10-16 19:12:43 +0200 | [diff] [blame] | 33 | |
| 34 | .. toctree:: |
| 35 | :maxdepth: 1 |
| 36 | |
Wenzel Jakob | 6ba9865 | 2016-10-24 23:48:20 +0200 | [diff] [blame] | 37 | overview |
jbarlow83 | 40db2c7 | 2017-02-02 04:56:31 -0800 | [diff] [blame] | 38 | strings |
Dean Moldovan | 67b52d8 | 2016-10-16 19:12:43 +0200 | [diff] [blame] | 39 | stl |
| 40 | functional |
| 41 | chrono |
| 42 | eigen |
| 43 | custom |