The Microsoft iostream Class Library provides a set of macros for creating parameterized manipulators. Manipulators with a single int or long parameter are a special case and will be discussed first.
Example 1
To create an output stream manipulator that accepts a single int or long parameter (like setw), you must use the OMANIP macro, defined in IOMANIP.H. Example 1 defines a fillblank manipulator that inserts a specified number of blanks into the stream:
// exios201.cpp
// A custom manipulator with an integer parameter
#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. This class definition includes a constructor and an overloaded ostream insertion operator for an object of the class. The fillblank function, inserted in the stream, calls the __OMANIP_int constructor in order to return an object of class __OMANIP_int. Thus fillblank can be used with an ostream insertion operator. The constructor, in turn, calls your fb function.
Note:
The OMANIP macro represents an advanced use of C++. It will ultimately be superseded once C++ accommodates parameterized types. In the meantime, it is easier to adapt the code above than to analyze the syntax. The expression OMANIP(long) expands to another built-in class, __OMANIP_long, which accommodates functions with a long integer parameter.