#include <iostream>
#include <string>
#include <sstream>

template < typename T > std::string to_string( const T& value )
{
    std::ostringstream os ;
    os << value ;
    return os.str() ;
}

template < typename T > T to_value( const std::string& str )
{
    std::istringstream is(str) ;
    T value ;
    if( is >> value >> std::ws && is.eof() ) // success
        return value ;
    else // failed
       // report error (throw something)
       return T() ;
}

int main()
{
     int i = 12345 ;
     std::string str = to_string(i) ;
     int j = to_value<int>(str) ;
     std::cout << "i: " << i << " str: " << str << " j: " << j << '\n' ;

     double d = 123.45 ;
     str = to_string(d) ;
     double e = to_value<double>(str) ;
     std::cout << "d: " << d << " str: " << str << " e: " << e << '\n' ;
}
