I have a vector of strings, each of which is the result of applying std::to_string to some basic datatype (eg char, int, double). I would like a function to undo this into a tuple of the appropriate types.
I have a simple function template to invert std::to_string:
template<typename T>
T from_string(std::string s)
{
}
template<>
int from_string<int>(std::string s)
{
return std::stoi(s);
}
template<>
double from_string<double>(std::string s)
{
return std::stod(s);
}
//... and more such specializations for the other basic types
I want a function like:
template<typename... Ts>
std::tuple<Ts> undo(const std::vector<std::string>>& vec_of_str)
{
// somehow call the appropriate specializations of from_string to the elements of vector_of_str and pack the results in a tuple. then return the tuple.
}
The function should behave like this:
int main()
{
auto ss = std::vector<std::string>>({"4", "0.5"});
auto tuple1 = undo<int, double>(ss);
std::tuple<int, double> tuple2(4, 0.5);
// tuple1 and tuple2 should be identical.
}
I think that I have to "iterate" over the parameters in Ts (perhaps the correct term is "unpack"), call the previous function, from_string for each one, and then package the results of each application of from_string into a tuple. I've seen (and used) examples that unpack a template parameter pack - they are usually recursive (but not in the usual way of a function calling itself), but I don't see how to do the rest.