Writing my own io manipulators with arguments

Hello! I hope I write at the right place. I would like to learn more how to create my own input/output manipulators with parameters. Without parameters is easy, but with them? I do not mean to override operator>> or operator<<, but to create my own manipulators like
1
2
3
4
5
ostream &manip(ostream &stream)
{
//some code
return stream;
}

but with parameters.
Thank you.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>

struct fw
{
   explicit constexpr fw( unsigned int w, char f = ' ' ) : width(w), fill(f) {}
   const unsigned int width ;
   const char fill ;

   template < typename CHAR, typename TRAITS > inline friend
   std::basic_ostream<CHAR,TRAITS>& operator<< ( std::basic_ostream<CHAR,TRAITS>& stm,
                                                  const fw& manip )
   {
       stm.width( manip.width );
       stm.fill( stm.widen( manip.fill ) ) ;
       return stm.flush() ;
   }
};

int main()
{
    std::cout << fw(10,'+') << 1234 << fw(8) << 567 << '\n';
}


See: http://www.angelikalanger.com/Articles/C++Report/Manipulators1.pdf
Last edited on
Topic archived. No new replies allowed.