Skip to content

Instantly share code, notes, and snippets.

@cfobel
Last active December 20, 2015 18:39
Show Gist options
  • Save cfobel/6177523 to your computer and use it in GitHub Desktop.
Save cfobel/6177523 to your computer and use it in GitHub Desktop.
Cython stack-allocated struct/class
struct Foo {
Foo() : x(0), y(0) {}
Foo(int x, int y) : x(x), y(y) {}
int x;
int y;
};
inline Foo test_return() {
Foo f(7, 4);
return f;
}
cdef extern from "Foo.hpp":
struct Foo:
int x
int y
Foo test_return()
def other():
cdef Foo f
cdef Foo bar = test_return()
print f.x, f.y
print bar.x, bar.y
@cfobel
Copy link
Author

cfobel commented Aug 7, 2013

NB The struct/class must have a default constructor to allow stack allocation or to return as an lvalue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment