I have a function that I would like to generalize. Simply put, I have std::string s
one that I am processing with a parser that generates std::vector<std::string>
(this is a list like in "1, 2, 3"
), and the function should return std::vector<T>
, with T
, restricted double
or int
.
The vector must contain the converted values.
I am stuck with the last parameter std::transform
, as it should switch between std::stod
and std::stoi
. The solution I'm looking for is template metaprogramming of magic, not if (std::is_same<T,int>::value)
.
Any clues?
template <class T>
auto get_vector(std::string s) -> std::vector<T>
{
std::vector<T> v;
auto tmp = split(s);
std::transform(tmp.begin(), tmp.end(), std::back_inserter(v), ??);
return v;
}
source
share