Dean Moldovan | f0b0df5 | 2016-10-19 22:13:27 +0200 | [diff] [blame] | 1 | Type conversions |
| 2 | ################ |
| 3 | |
Wenzel Jakob | 6ba9865 | 2016-10-24 23:48:20 +0200 | [diff] [blame] | 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. |
Dean Moldovan | f0b0df5 | 2016-10-19 22:13:27 +0200 | [diff] [blame] | 9 | |
Wenzel Jakob | 6ba9865 | 2016-10-24 23:48:20 +0200 | [diff] [blame] | 10 | 1. Use a native C++ type everywhere. In this case, the type must be wrapped |
| 11 | using pybind11-generated bindings so that Python can interact with it. |
Dean Moldovan | f0b0df5 | 2016-10-19 22:13:27 +0200 | [diff] [blame] | 12 | |
Wenzel Jakob | 6ba9865 | 2016-10-24 23:48:20 +0200 | [diff] [blame] | 13 | 2. Use a native Python type everywhere. It will need to be wrapped so that |
| 14 | C++ functions can interact with it. |
Dean Moldovan | f0b0df5 | 2016-10-19 22:13:27 +0200 | [diff] [blame] | 15 | |
Wenzel Jakob | 6ba9865 | 2016-10-24 23:48:20 +0200 | [diff] [blame] | 16 | 3. Use a native C++ type on the C++ side and a native Python type on the |
| 17 | Python side. pybind11 refers to this as a *type conversion*. |
Dean Moldovan | f0b0df5 | 2016-10-19 22:13:27 +0200 | [diff] [blame] | 18 | |
Wenzel Jakob | 6ba9865 | 2016-10-24 23:48:20 +0200 | [diff] [blame] | 19 | Type conversions are the most "natural" option in the sense that native |
| 20 | (non-wrapped) types are used everywhere. The main downside is that a copy |
| 21 | of the data must be made on every Python ↔ C++ transition: this is |
| 22 | needed since the C++ and Python versions of the same type generally won't |
| 23 | have the same memory layout. |
Dean Moldovan | f0b0df5 | 2016-10-19 22:13:27 +0200 | [diff] [blame] | 24 | |
Wenzel Jakob | 6ba9865 | 2016-10-24 23:48:20 +0200 | [diff] [blame] | 25 | pybind11 can perform many kinds of conversions automatically. An overview |
| 26 | is provided in the table ":ref:`conversion_table`". |
Dean Moldovan | f0b0df5 | 2016-10-19 22:13:27 +0200 | [diff] [blame] | 27 | |
Wenzel Jakob | 6ba9865 | 2016-10-24 23:48:20 +0200 | [diff] [blame] | 28 | The following subsections discuss the differences between these options in more |
| 29 | detail. The main focus in this section is on type conversions, which represent |
| 30 | the last case of the above list. |
Dean Moldovan | 67b52d8 | 2016-10-16 19:12:43 +0200 | [diff] [blame] | 31 | |
| 32 | .. toctree:: |
| 33 | :maxdepth: 1 |
| 34 | |
Wenzel Jakob | 6ba9865 | 2016-10-24 23:48:20 +0200 | [diff] [blame] | 35 | overview |
jbarlow83 | 40db2c7 | 2017-02-02 04:56:31 -0800 | [diff] [blame^] | 36 | strings |
Dean Moldovan | 67b52d8 | 2016-10-16 19:12:43 +0200 | [diff] [blame] | 37 | stl |
| 38 | functional |
| 39 | chrono |
| 40 | eigen |
| 41 | custom |
| 42 | |