Created
January 29, 2013 17:34
-
-
Save kbenzie/4666030 to your computer and use it in GitHub Desktop.
Safely convert string to template parameter type T
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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