It is well known that the user can define stream manipulators like this:
ostream& tab(ostream & output)
{
return output<< '\t';
}
And this can be used in main() like this:
cout<<'a'<<tab<<'b'<<'c'<<endl;
Please explain me how does this all work? If operator<< assumes as a second parameter a pointer to the function that takes and returns ostream &, then please explain my why it is necessary? What would be wrong if the function does not take and return ostream & but it was void instead of ostream &?
Also it is interesting why “dec”, “hex” manipulators take effect until I don’t change between them, but user defined manipulators should be always used in order to take effect for each streaming?
The standard defines the following
operator<<overload in thebasic_ostreamclass template:The parameter is a pointer to a function taking and returning a reference to a
std::ostream.This means that you can “stream” a function with this signature to an
ostreamobject and it has the effect of calling that function on the stream. If you use the name of a function in an expression then it is (usually) converted to a pointer to that function.std::hexis anstd::ios_basemanipulator defined as follows.This means that streaming
hexto anostreamwill set the output base formatting flags to output numbers in hexadecimal. The manipulator doesn’t output anything itself.