iostream クラス ライブラリには、パラメーター化したマニピュレーターを作成するためのマクロ セットが用意されています。 単一の int
引数または long
引数のみを取るマニピュレーターは特殊なケースです。 単一の int
引数または long
引数を受け取る出力ストリーム マニピュレーター (setw
など) を作成するには、<iomanip> で定義される _Smanip マクロを使用する必要があります。 次の例では、指定した数の空白をストリームに挿入する fillblank
マニピュレーターを定義します。
例
// 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";
}