Output Stream Manipulators with One Argument (int or long)

 

The new home for Visual Studio documentation is Visual Studio 2017 Documentation on docs.microsoft.com.

The latest version of this topic can be found at Output Stream Manipulators with One Argument (int or long).

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 _Smanip macro, which is defined in <iomanip>. This example defines a fillblank manipulator that inserts a specified number of blanks into the stream:

// output_stream_manip.cpp  
// compile with: /GR /EHsc  
#include <iostream>  
#include <iomanip>  
using namespace std;  
  
void fb( ios_base& os, int l )  
{  
   ostream *pos = dynamic_cast<ostream*>(&os);  
   if (pos)  
   {  
      for( int i=0; i < l; i++ )  
      (*pos) << ' ';  
   };  
}  
  
_Smanip<int>  
   __cdecl fillblank(int no)  
   {     
   return (_Smanip<int>(&fb, no));  
   }  
  
int main( )  
{  
   cout << "10 blanks follow" << fillblank( 10 ) << ".\n";  
}  

Custom Manipulators with Arguments

Show: