How to declare a C++ function that can take any python iterable?

28 Views Asked by At

We have functions like

void myf(const boost::python::list& l) {...}

is there a way to accept any python iterable, or less generically, any collection?

1

There are 1 best solutions below

0
danielm103 On

There’s boost::python::stl_input_iterator, so you can do something like

template<typename T>
inline std::vector< T > py_list_to_std_vector(const boost::python::object& iterable)
{
    return std::vector< T >(boost::python::stl_input_iterator< T >(iterable),
        boost::python::stl_input_iterator< T >());
}