Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Gwenaël Samain
cython
Commits
d1d120e4
Commit
d1d120e4
authored
Jul 08, 2012
by
Stefan Behnel
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
documentation on C++ STL container coercion
parent
a1318767
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
39 additions
and
0 deletions
+39
-0
docs/src/userguide/wrapping_CPlusPlus.rst
docs/src/userguide/wrapping_CPlusPlus.rst
+39
-0
No files found.
docs/src/userguide/wrapping_CPlusPlus.rst
View file @
d1d120e4
...
@@ -410,6 +410,45 @@ For example::
...
@@ -410,6 +410,45 @@ For example::
The pxd files in ``/Cython/Includes/libcpp`` also work as good examples on
The pxd files in ``/Cython/Includes/libcpp`` also work as good examples on
how to declare C++ classes.
how to declare C++ classes.
Since Cython 0.17, the STL containers coerce from and to the
corresponding Python builtin types. The conversion is triggered
either by an assignment to a typed variable (including typed function
arguments) or by an explicit cast, e.g.::
from libcpp.string cimport string
from libcpp.vector cimport vector
cdef string s = py_bytes_object
print(s)
cpp_string = <string> py_unicode_object.encode('utf-8')
cdef vector[int] vect = xrange(1, 10, 2)
print(vect) # [1, 3, 5, 7, 9]
cdef vector[string] cpp_strings = b'ab cd ef gh'.split()
print(cpp_strings.get(1)) # b'cd'
The following coercions are available:
+------------------+----------------+-----------------+
| Python type => | *C++ type* | => Python type |
+==================+================+=================+
| bytes | std::string | bytes |
+------------------+----------------+-----------------+
| iterable | std::vector | list |
+------------------+----------------+-----------------+
| iterable | std::list | list |
+------------------+----------------+-----------------+
| iterable | std::set | set |
+------------------+----------------+-----------------+
| iterable (len 2) | std::pair | tuple (len 2) |
+------------------+----------------+-----------------+
All conversions create a new container and copy the data into it.
The items in the containers are converted to a corresponding type
automatically, which includes recursively converting containers
inside of containers, e.g. a C++ vector of maps of strings.
Exceptions
Exceptions
-----------
-----------
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment