[Cython] Class methods returning C++ class references are not dealt with correctly?

Yury V. Zaytsev yury at shurup.com
Thu Feb 28 13:58:08 CET 2013


Hi,

I'm sorry if my question would appear to be trivial, but what am I
supposed to do, if I want to wrap class methods, that return a reference
to another class?

>From reading the list, I've gathered that apparently the best strategy
of dealing with references is just to not to use them (convert to
pointers immediately), because of some scoping rules issues.

It works for me for a simple case of POD types, like

    cdef extern from "test.h":
        int& foo()

    cdef int* x = &foo()

but in a more complex case, Cython generates incorrect C++ code (first
it declares a reference, then assigns to it, which, of course, doesn't
even compile):

    cdef extern from "token.h":
        cppclass Token:
            Token(const Datum&) except +

    cdef extern from "tokenstack.h":
        cppclass TokenStack:
            Token& top() except +

    cdef Token* tok = &self.pEngine.OStack.top()

<->

    Token *__pyx_v_tok;
    Token &__pyx_t_5;
    __pyx_t_5 = __pyx_v_self->pEngine->OStack.top();
    __pyx_v_tok = (&__pyx_t_5);

I would expect to see this instead:

    Token *__pyx_v_tok = &__pyx_v_self->pEngine->OStack.top();

Am I doing something wrong? Is there any other way to achieve what I
want, other than writing custom C macros?

Thanks,

-- 
Sincerely yours,
Yury V. Zaytsev




More information about the cython-devel mailing list