Commit e98f32aa authored by Robert Bradshaw's avatar Robert Bradshaw

Note on C++ auto and stl iteration.

parent b62ded56
......@@ -209,12 +209,12 @@ Declare a var with the wrapped C++ class
Now, we use cdef to declare a var of the class with the C++ ``new`` statement::
cdef Rectangle *rec = new Rectangle(1, 2, 3, 4)
rec_ptr = new Rectangle(1, 2, 3, 4)
recArea = rec.getArea()
recArea = rec_ptr.getArea()
del rec # delete heap allocated object
del rec_ptr # delete heap allocated object
It's also possible to declare a stack allocated object, as long as it has
a "default" constructor::
......@@ -508,7 +508,7 @@ comprehensions). For example, one can write::
cdef vector[int] v = ...
for value in v:
return [x in v if x % 2 == 0]
return [x*x for x in v if x % 2 == 0]
Simplified wrapping with default constructor
......@@ -625,6 +625,22 @@ functions as references (const or otherwise) as it has no impact on the
caller's syntax.
``auto`` Keyword
Though Cython does not have an ``auto`` keyword, Cython local variables
not explicitly typed with ``cdef`` are deduced from the types of the right hand
side of *all* their assignments (see the ``infer_types``
:ref:`compiler directive <compiler-directives>`). This is particularly handy
when dealing with functions that return complicated, nested, templated types,
cdef vector[int] v = ...
it = v.begin()
(Though of course the ``for .. in`` syntax is prefered for objects supporting
the iteration protocol.)
Caveats and Limitations
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment