Original Post
I'm trying to create a template function to convert text to various data types: However, I cannot get the entire buffer to fill the string. It only contains "7" instead of "7 Hello World". I realize I can use the str() member function of stringstream, but I want the solution to be general. Is there a way to accomplish this?
template<typename Output, typename Input>
Output lexical_cast(Input &input) {
std::stringstream ss;
ss << input;
Output output;
ss >> output;
return output;
}
int main () {
using namespace std;
string s;
int i;
char buf[] = "7 Hello World";
s = lexical_cast<string>(buf);
i = lexical_cast<int>(buf);
cout << "String: " << s << " Int: " << i;
cin.get();
return 0;
}