There is nothing wrong with it except there is no overloaded << operator defined for it. The existing overloads for << are expecting a manipulator with the signature ostream& (*fp)(ostream&).
If you gave it a manipulator with the type ostream& (*fp)() you would get a compiler error since it does not have a definition for operator<<(ostream&, ostream& (*fp)()). If you wanted this functionality you would have to overload the << operator to accept manipulators of this type.
You would have to write a definition for this:
ostream& ostream::operator<<(ostream& (*m)())
Keep in mind here that nothing magical is happening here. The stream libraries rely heavily on standard C++ features: operator overloading, classes, and references.
Now that you know how you can create the functionality you described, here's why we don't:
Without passing a reference to the stream we are trying to manipulate, we can't make modifications to the stream connected to the final device (cin, out, err, fstream, etc). The function (modifiers are all just functions with fancy names) would either have to return a new ostream that had nothing to do with the one to the left of the << operator, or through some very ugly mechanism, figure out which ostream it should connect with else everything to right of the modifier won't make it to the final device, but would rather be sent to whatever ostream the function/modifier returned.
Think of streams like this
cout << "something here" << tab << "something else"<< endl;
really means
(((cout << "something here") << tab ) << "something else" ) << endl);
where each set of parentheses does something to cout (write, modify etc) and then returns cout so the next set of parentheses can work on it.
If your tab modifier/function did not take a reference to an ostream it would have to somehow guess what ostream was to the left of the << operator to perform its task. Were you working with cour, cerr, some file stream...? The internals of the function will never know unless they are handed that information somehow, and why not that how to be as simple as a reference to it.
Now to really drive the point home, let's look at what endl really is and which overloaded version of the << operator we are using:
This operator looks like this:
ostream& ostream::operator<<(ostream& (*m)(ostream&))
{
return (*m)(*this);
}
endl looks like this:
ostream& endl(ostream& os)
{
os << '\n';
os.flush();
return os;
}
The purpose of endl is to add a newline and flush the stream, making sure all the contents of the stream’s internal buffer have been written to the device. In order to do this, it first needs to write a '\n' to this stream. It then needs to tell the stream to flush. The only way for endl to know which stream to write to and flush is for the operator to pass that information to the endl function when it calls it. It'd be like me telling you to wash my car, but never tell you which car is mine in the full parking lot. You'd never be able to get your job done. You need me to either hand you my car, or I can wash it myself.
I hope that clears things up
PS - If you do happen to accidentally find my car, please wash it.
tab
just prints a tab, it doesn't alter the stream's settings likehex
does. – Oswelloperator<<
thatostream
contains for function pointers so it wouldn't compile. – Oswellostream
as argument, the function body would have no reference to the stream object on which to operate which would make it difficult for it to do anything. – Oswelloperator<<
overload, it passes itself to the manipulator. "Returns:pf(*this)
."std::cout << tab(cout)
would be wrong as it would be trying to call anop<<
taking twoostream
. Theop<<
we are talking about takes a function pointer, not the result of calling a function. – Oswell