Created
May 10, 2016 13:44
-
-
Save danielSanchezQ/c5301359d6509ef77f9f9977c829cdde to your computer and use it in GitHub Desktop.
how to wrapp string numeric conversion methods, c++11
This file contains 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 <iostream> | |
#include <functional> | |
#include <string> | |
template<class T> | |
T getFromString(const std::string& value, std::function<T(const std::string&)> f) | |
{ | |
return f(value); | |
} | |
auto getIntFromString = std::bind(getFromString<int>, std::placeholders::_1, [](const std::string& s)->int {std::stoi(s);}); | |
auto getFloatFromString = std::bind(getFromString<float>, std::placeholders::_1, [](const std::string& s)->float{std::stof(s);}); | |
int main() { | |
std::cout << getIntFromString("12") << std::endl; | |
std::cout << getFloatFromString("12.4556") << std::endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment