This is an exercise for the school, so please provide only tips and complete examples; -)
I have my own manipulator:
template<typename T, typename Tr=char_traits<T> > ios_base& toggle(basic_ios<T,Tr>& io) { if(io.flags() & ios::scientific) { io.unsetf(ios::scientific); io.flags(ios::fixed); } else { io.unsetf(ios::fixed); io.flags(ios::scientific); } return io; }
I wrote this because I need to write a manipulator with the form ios_base& my_manip(basic_ios&) .
If I use it like this (without using the return value):
toggle(cout);
... it works great. But if I use it like this:
toggle(cout) << 54444.6456555 << endl;
This does not work (since std :: ios_base does not have a <<() operator, as indicated below).
In general, I do not understand that ios_base& my_manip(basic_ios&) may be useful for ... Do you have a hint / example?
You guys have helped me a lot! What I still DO NOT understand is the motivation to pass basic_ios and return ios_base (because it is proposed to do this in an exercise that I have to solve ...). What could be a possible scenario for using this?
source share