Converting std::list to C friendly type

c, c++, stl

Solution

Copy the `std::list` to a `std::vector` and return the address of the first element, as you already mentioned.

(Of course, this may mean that you don't want to be using a `std::list` in the first place.)

(This solution assumes that the object being accessed is owned by the C++ library -- If this isn't the case, you may need to consider allocating the memory from your C code and passing a pointer into the C++ library to copy the data.)

Problem

What's the most elegant way to return a `std::list` object from a shared lib function (implemented by C++ code) to a C consumer? I know for `std::vector`, we can return the address of the 1st element of the vector and have the consumer treat it as an array, but std::list is implemented as a linked lis.

Original source

Related problems