user1042840
user1042840

Reputation: 1945

what does it mean: 'std::ios_base& (*f)(std::ios_base&))'

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?

Upvotes: 3

Views: 458

Answers (3)

James Kanze
James Kanze

Reputation: 153929

It's a pointer to a function which takes a std::ios_base& as the argument, 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:

from_string( anInt, "0A", &std::hex );

Upvotes: 5

hmjd
hmjd

Reputation: 121971

It is a function pointer named f that returns a std::ios_base& and accepts std::ios_base& as its single argument.

Upvotes: 1

Violet Giraffe
Violet Giraffe

Reputation: 33599

That is a declaration of a pointer f to a function that is taking std::ios_base& and returning std::ios_base&.

Upvotes: 2

Related Questions