consider following code:
class c{}; std::ostream &operator <<(std::ostream &o, const c &) { o.fill('!'); o.width(8); return o << 42; } int main() { std::cout << c{} << '\n'; std::cout << 42 << '\n'; return 0; } it outputs:
!!!!!!42
42
i expecting !!!!!!42 twice because i've changed state of provided std::ostream o calling fill , width inside operator <<, used think fill character , width setted operator leak outside operator call if sticky properties.
as can see don't flush stream nor re-set fill character or width so, why (and how) original behaviour preserved?
so question is: how properties of ostream setted previous state after call operator<< class c?
this doesn't bothers me, i'm happy behaviour want understand how works.
as underscore_d mentioned: width isn't sticky. there no such attribute stickiness iostream classes , manipulators.
though, if width not have been reset previous << operator call, width affect output of \n:
std::cout << std::setw(10) << std::setfill('!') << 42 << '\n'; std::cout << std::setw(10) << std::setfill('!') << 42 << std::setw(10) << '\n'; gives
!!!!!!!!42 !!!!!!!!42!!!!!!!!! .
Comments
Post a Comment