You may want something like that:
#include <string>
#include <sstream>
namespace details
{
template <typename T>
struct stringToTypeImpl
{
void operator () (std::stringstream& ss, T& t) const
{
ss >> t;
}
};
// And some specializations
template <typename T, int W, int H>
struct stringToTypeImpl<Eigen::Matrix<T, W, H> >
{
void operator () (std::stringstream& ss, Eigen::Matrix<T, W, H>& t) const
{
for (int j = 0; j != H; ++j) {
for (int i = 0; i != W; ++i) {
stringToTypeImpl<T>()(ss, t(i, j)); //ss >> t(i, j);
}
}
}
}
// ...
}
template <typename T>
void stringToType(const std::string& s, T& t)
{
std::stringstream ss(s);
details::stringToTypeImpl<T>()(ss, t);
}
int main() {
std::string s = "42";
int i;
stringToType(s, i);
return 0;
}