The iostream class library provides a set of macros for creating parameterized manipulators. Manipulators with a single int or long argument are a special case. To create an output stream manipulator that accepts a single int or long argument (like setw), you must use the OMANIP macro, which is defined in IOMANIP.H. This example defines a fillblank
manipulator that inserts a specified number of blanks into the stream:
#include <iostream.h>
#include <iomanip.h>
ostream& fb( ostream& os, int l )
{
for( int i=0; i < l; i++ )
os << ' ';
return os;
}
OMANIP(int) fillblank( int l )
{
return OMANIP(int) ( fb, l );
}
void main()
{
cout << "10 blanks follow" << fillblank( 10 ) << ".\n";
}
The IOMANIP.H header file contains a macro that expands OMANIP(int) into a class, __OMANIP_int, which includes a constructor and an overloaded ostream insertion operator for an object of the class. In the previous example, the fillblank
function calls the __OMANIP_int constructor to return an object of class __OMANIP_int. Thus, fillblank
can be used with an ostream insertion operator. The constructor calls the fb
function. The expression OMANIP(long) expands to another built-in class, __OMANIP_long, which accommodates functions with a long integer argument.