Skip to content

Instantly share code, notes, and snippets.

@jackeylu
Created July 24, 2013 16:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jackeylu/6071945 to your computer and use it in GitHub Desktop.
Save jackeylu/6071945 to your computer and use it in GitHub Desktop.
a stringstream example for converting any built-in type or self-defined with _str method
#include <sstream> // for stringstream
#include <iostream>
template <class T>
T convert(const char* arg)
{
T result;
std::stringstream ss;
ss << arg;
ss >> result;
return result;
}
int test_convert(int argc, const char* argv[])
{
int p1 = convert<int>(argv[1]);
double p2 = convert<double>(argv[2]);
std::cout << "parameter 1 = " << p1 << std::endl;
std::cout << "parameter 2 = " << p2 << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment