I want to implement a function like this:
template <class C>
C string_to_number(const string &s){
stringstream ss(s);
C num;
ss >> num;
return num;
}
but it cause error. I can do this without class C but only one data type like double string_to_number(const string &s) or int string_to_number(const string &s) but I can't have both at the same time.
How can I do this to use the function like this:
int main()
{
string s = "12.75";
double d = string_to_number(s); // be 12.75
int i = string_to_number(s); // be 12
}
anybody knows there is any way to do it ?