Search code examples
cython

How to declare a c++ tuple in cython


I have a C++ method declared as follow:

std::tuple<std::vector<int>, std::size_t, std::size_t> get_state(); // Should I use a struct and expose the struct instead?

And I would like to declare a cython extension to interface it in Python

cdef extern from "cpp_sources/CClass.hpp":
    cdef cppclass CClass nogil:
        tuple[vector[int], size_t, size_t] get_state()

Unfortunately, I don't see an easy import to make to have access to a C++ tuple. I also checked here with no success.

My question is, is there an easy way to have access to a c++ tuple? Or maybe there is a better way to have access to some elements?

(We don't care about performances for this exact method)


Solution

  • Unfortunately this is not supported. More generally variadic templates are not supported - that's why you have pair for example, but not a generic tuple.

    In the github issue I linked they have their own version of a workaround, which is what I would come up with first - for every amount of N arguments I will actually use,

    template<typename T_1, ... typename T_N>
    using tupleN = std::tuple<T_1, ..., TN>;
    

    and exporting each tupleN individually. There is no magic I'm aware of to make a general variadic template here.