C + + IO manipulator

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.