Skip to content

error: taking address of rvalue in for loops over C++ function results #3663

@louisabraham

Description

@louisabraham

context that is not a minimal reproducing example

I have a C++ function that returns a map<vector, int> and I want to convert it to python.
Obviously lists are not hashable so I convert the map to a list of pairs.

code

cdef extern from "mylib.hpp":
    map[vector[bool], int] myfun()

def f():
    out = []
    for pair in myfun():
        out.append(pair)
    return out

error

error: taking address of rvalue

When looking at the generated code:

  __pyx_t_3 = &myfun();
  __pyx_t_2 = __pyx_t_3->begin();
  for (;;) {
    if (!(__pyx_t_2 != __pyx_t_3->end())) break;
    __pyx_t_4 = *__pyx_t_2;
    ++__pyx_t_2;
    __pyx_v_pair = __pyx_t_4;

fix

If I assign the map first, it works:

def f():
    c_result = myfun()
    out = []
    for pair in c_result:
        out.append(pair)
    return out

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions