Skip to content

Instantly share code, notes, and snippets.

@danielSanchezQ
Created May 10, 2016 13:44
Show Gist options
  • Save danielSanchezQ/c5301359d6509ef77f9f9977c829cdde to your computer and use it in GitHub Desktop.
Save danielSanchezQ/c5301359d6509ef77f9f9977c829cdde to your computer and use it in GitHub Desktop.
how to wrapp string numeric conversion methods, c++11
#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