Re: [Cython] wrapping c++
Neal Becker wrote:
>> Neal Becker wrote:
>> > Is operator the real issue here? In the std::vector example, there is
>> > 'at',
>> > which does (almost) the same thing as operator[]. My problem is that
>> > both 'at' and 'operator[]' return int&, and to use them you need to
>> > say 'at[i]=j', which I don't know how to do in cython (except for the
>> > macro
>> > version above, or to write additional wrapper code in more c++
>> headers).
>
> Let's say we have a c++ class intvec, it has
> int& at(int index);
I just looked that up, int& is a C++ reference here (didn't know that
notation). What I would do is: create a header file and use it to define
macros that do C++ operations on references in a function-like style.
Things like the SET() 'function' that William proposed.
Then, you define these macros as being external functions in a .pxd file,
and cimport them into your .pyx file. Then you can use them in your Cython
code as normal functions, and Cython will generate the macro calls for
you, that the C preprocessor will translate into the correct operations.
As Robert suggested, enhancing Cython's syntax to understand references
would be helpful in the long term.
As for the return type of your at() function, have you tried declaring it
as returning "int*" instead? What does Cython do in this case?
(Continue reading)