Manipulators that do not have any parameters, like endl, are the simplest form of manipulator. The manipulator type manipT is a function pointer type, the manipulator Manip is the function pointer, and the associated function fmanipT() is the function pointed to.
In iostreams, the following function pointer types serve as manipulators:
(1) ios_base& (*pf)(ios_base&) (2) basic_ios<charT,Traits>& (*pf)(basic_ios<charT,Traits>) (3) basic_istream<charT,Traits>& (*pf)(basic_istream<charT,Traits>) (4) basic_ostream<charT,Traits>& (*pf)(basic_ostream<charT,Traits>)
The signature of a manipulator function is not limited to the examples above. If you have created your own stream types, you will certainly want to use additional signatures as manipulators.
For the four manipulator types listed above, the stream classes already offer the required overloaded inserters and member functions. For input streams, extractors take the following form:
template<class charT, class traits> basic_istream<charT, traits>& basic_istream<charT, traits>::operator>> (basic_istream<charT,traits>& (*pf)(input_stream_type&) ) { return (*pf)(*this);..}
where input_stream_type is one of the function pointer types (1)-(3).
Similarly, for output streams we have:
template<class charT, class traits> basic_ostream<charT, traits>& basic_ostream<charT, traits>::operator<< (basic_ostream<charT, traits>& (*pf)(output_stream_type& )) { return (*pf)(*this); }
where output_stream_type is one of the function pointer types (1), (2), or (4).
Let's look at the manipulator endl as an example of a manipulator without parameters. The manipulator endl, which can be applied solely to output streams, is a pointer to the following function of type (4):
template<class charT, class traits> inline basic_ostream<charT, traits>& endl(basic_ostream<charT, traits>& os) { os.put( os.widen('\n') ); os.flush(); return os; }
Hence an expression like:
cout << endl;
results in a call to the inserter:
ostream& ostream::operator<< (ostream& (*pf)(ostream&))
with endl as the actual argument for pf. In other words, cout << endl; is equal to cout.operator<<(endl);
Here is another manipulator, boolalpha, that can be applied to input and output streams. The manipulator boolalpha is a pointer to a function of type (1):
ios_base& boolalpha(ios_base& strm) { strm.setf(ios_base::boolalpha); return strm; }
NOTE: Every function that takes a reference to an ios_base, a basic_ios, a basic_ostream, or a basic_istream, and returns a reference to the same stream, can be used as a parameter-less manipulator.
The manipulator endl is often used for inserting the end-of-line character into a stream. However, endl does additionally flush the output stream, as you can see from the implementation of endl shown above. Flushing a stream, a time-consuming operation that decreases performance, is unnecessary in most common situations. In the standard example:
cout << "Hello world" << endl;
flushing is not necessary because the standard output stream cout is tied to the standard input stream cin, so input and output to the standard streams are synchronized anyway. Since no flush is required, the intent is probably to insert the end-of-line character. If you consider typing '\n' more trouble than typing endl, you can easily add a simple manipulator nl that inserts the end-of-line character, but refrains from flushing the stream.
OEM Edition, ©Copyright 1999, Rogue Wave Software, Inc.
Contact Rogue Wave about documentation or support issues.