I was looking for the best way to convert string to int and I came across a function I don’t understand:
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();
}
I know what template is, I don’t know what it means:
std::ios_base& (*f)(std::ios_base&)
Is a new pointer being created here, why are there 2 expressions enclosed in parenthesis next to each other?
It’s a pointer to a function which takes a
std::ios_base&as theargument, and returns an
std::ios_base&.In fact, it’s one form of a manipulator which doesn’t take any
arguments. The
<<overload for this type simply calls the function;the function then does whatever it likes on the stream, returning it.
Your function can thus be called with something like: