Skip to content

Instantly share code, notes, and snippets.

@kbenzie
Created January 29, 2013 17:34
Show Gist options
  • Save kbenzie/4666030 to your computer and use it in GitHub Desktop.
Save kbenzie/4666030 to your computer and use it in GitHub Desktop.
Safely convert string to template parameter type T
#include <string>
#include <sstream>
// Convert string to type T
template <class T> bool from_string(T& t, const std::string& s, std::ios_base& (*f)(std::ios_base&)) {
std::istringstream iss(s);
return !(iss >> f >> t).fail();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment