I want to figure out how to pass a manipulator like std::endl
to a function and then use the passed-in manipulator in the function. I can declare the function like this:
void f(std::ostream&(*pManip)(std::ostream&));
and I can call it like this:
f(std::endl);
That's all fine. My problem is figuring out how to use the manipulator inside f
. This doesn't work:
void f(std::ostream&(*pManip)(std::ostream&))
{
std::cout << (*pManip)(std::cout); // error
}
Regardless of compiler, the error message boils down to the compiler not being able to figure out which operator<<
to call. What do I need to fix inside f
to get my code to compile?
std::cout << pManip;
– Lentissimostd::cout << pManip
will print the address of the function. (pManip
is a function pointer.) See Sveltely's answer for the proper code. – Egocentrism