Writing my own io manipulators with arguments
Jul 22, 2012 at 10:45am UTC
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.
Jul 22, 2012 at 12:06pm UTC
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 Jul 22, 2012 at 12:13pm UTC
Topic archived. No new replies allowed.