Rogue Wave banner
Previous fileTop of DocumentContentsIndexNext file

12.2 Manipulators without Parameters

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:

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:

where input_stream_type is one of the function pointer types (1)-(3).

Similarly, for output streams we have:

where output_stream_type is one of the function pointer types (1), (2), or (4).

12.2.1 Examples of Manipulators without Parameters

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):

Hence an expression like:

results in a call to the inserter:

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):


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.

12.2.2 A Remark on the Manipulator endl

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:

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.


Previous fileTop of DocumentContentsIndexNext file

OEM Edition, ©Copyright 1999, Rogue Wave Software, Inc.
Contact Rogue Wave about documentation or support issues.