1
I have tuples with objects that can have different types. How I would generalize the following code:
#include <iostream>
std::vector<void *> to_vector(std::tuple<int, double, std::string> x)
{
std::vector<void *> out;
out.push_back((void *)new int(std::get<0>(x)));
out.push_back((void *)new double(std::get<1>(x)));
out.push_back((void *)new std::string(std::get<2>(x)));
return out;
}
int main()
{
std::tuple<int, double, std::string> x = {1, 2, "hi"};
std::vector<void *> v = to_vector(x);
for (auto i : v)
{
std::cout << i << std::endl;
}
}
That is, I would like the function to_vector
could receive any tuple and return an array of pointers without explicitly writing all the push_backs
. I imagine this is possible using templates
and some variation of what was done here, but I could not at all.
you’ve already taken a look therein? I haven’t been able to test it yet, but maybe it’ll help you.
– Junior Nascimento
the problem is that apparently this assumes that all elements of the tuple have the same type, which does not happen in my case.
– Daniel Falbel