I have to pass a Python deque to a C++ function but I can't find any documentation on how to convert a deque to a C++ container (for this case, either a deque or a vector would do). Searched everywhere but I couldn't find a solution. There's gotta be a simple way to do this right? Thanks
Asked
Active
Viewed 174 times
1
-
https://en.cppreference.com/w/cpp/container/deque – Daniel Jun 22 '20 at 21:52
-
that site says nothing about how to copy a Python deque to a C++ deque? – qwerty_99 Jun 22 '20 at 22:07
-
One basic issue is that a c++ container holds a specific type, while a Python container can hold any type. You have to decide what you want to do about that. – DavidW Jun 23 '20 at 11:15
1 Answers
1
There's no "simple way". You'll need to do that by hand (or maybe with some kind of interface generator), in a data-specific way.
Take a look at C API Introduction — Python 3.8.3 documentation for an overview of Python data model at C level. It has absolutely nothing to do with any C++ types.
You'll need to somehow extract the data you need from Python objects, via Python C API, and construct the C++ structure you need from that.
SWIG may help you here. Take a look at SWIG-4.0 Documentation -- it has a section about how to do conversion between C++ types and those from a scripting language.

ivan_pozdeev
- 33,874
- 19
- 107
- 152
-
thanks! is this compatible with other tools like boost and the standard API? – qwerty_99 Jun 23 '20 at 00:05
-
AFAICS, SWIG acts in a library-independent manner. So it doesn't matter what specific types you are dealing with -- standard, boost or whatever else. – ivan_pozdeev Jun 23 '20 at 00:25