toString is correct, toValue should look like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
template <class T>
T toValue( string str )
{
stringstream ss;
ss << str;
T retValue;
// If it's possible to convert the string to T
if ( ss >> retValue )
{
// return the result
return retValue;
}
// Else return the default value of T
return T();
}
Then in main() you can call them like this:
1 2 3 4
string str = "1234";
int value = toValue < int > ( str ); // value now holds 1234
string str2 = toString( value ); // str2 now holds "1234"