Skip to content

How can I create a make_tuple from a vector<T> whose length is unknown? #3952

Answered by Skylion007
GoingMyWay asked this question in Q&A
Discussion options

You must be logged in to vote

You can just use the API for casting back and forth. There are several options:

Most straight forward is to just the py::tuple ctor with the list size:

auto my_tuple = py::tuple(p.size());
... fill your tuple with a loop etc ...

You should be able to convert from to a py::list and convert that to a py::tuple using casting

auto my_tuple = py::tuple(py::cast(p));

Once casting back to a STL vector is trivial using the same APIs.

auto vector = my_tuple.cast<std::vector<int>>();

You could even populate the std::vector manually by iterating through the tuple in a for loop.

Replies: 4 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by Skylion007
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
4 participants