C + + IO Manipulatori Use this kind of Io manipular now and then.
Let's look at STD::SETW first, it ' s defined as:
_MRTIMP2 _smanip<streamsize> __cdecl SETW (streamsize wide) {//manipulator to set Widthreturn (_Smanip< Streamsize> (&swfun, Wide));}
Swfun is the function which do the real job, it would call Cout.width (wide), it ' s defined as:
FUNCTION setwstatic void __cdecl swfun (ios_base& iostr, streamsize wide) {//Set widthiostr.width (wide);}
Now let's look at _smanip, it's a simple template struct, it contains a function pointer which would set property of Ios_ Base and an template arg
TEMPLATE STRUCT _smaniptemplate<class _arg>struct _smanip{//store function pointer and argument Value_smanip (VO ID (__cdecl *_left) (ios_base&, _arg), _arg _val): _pfun (_left), _manarg (_val) {//construct from function pointer and a Rgument value}void (__cdecl *_pfun) (ios_base&, _arg);//The function Pointer_arg _manarg;//the argument value};
_pfun is Swfun, _arg was wideat last, Iomanip override operator<<
Template<class _elem,class _traits,class _arg> Inlinebasic_ostream<_elem, _Traits>& operator<< (Basic_ostream<_elem, _traits>& _ostr, const _smanip<_arg>& _MANIP) {//Insert by calling function with output stream and argument (*_manip._pfun) (_ostr, _manip._manarg); return (_OSTR);}
In short, iomanipular define a function which does the set job, put this function and arg in a template struct, then Overri De operator<<, get function pointer and arg from this
struct, then set property of iOS.
C + + IO manipulator