c++11 - C++ copy stream manipulator to other stream -



c++11 - C++ copy stream manipulator to other stream -

imagine std::ostream& operator<< wants stuff numbers. purpose, might want utilize std::hex, other may want utilize none, whatever, manipulator possible.

how re-create std::ostream without text-content of ostream passed argument? need manipulators only.

so want std::cout << std::hex << somecoolclass(10), somecoolclass like

struct somecoolclass { somecoolclass(int i) : _i(i) {} friend std::ostream& operator<<(std::ostream& os, const somecoolclass& rhs) { std::stringstream ss; //magically re-create manipulators of os ss << _i; homecoming os << ss.str(); } private: int _i; };

prints a. know illustration useless , other stream convert integer string seems useless lets imagine isn't useless , not pure nonsene.

thanks.

ios::copyfmt

friend std::ostream& operator<<(std::ostream& os, const somecoolclass& rhs) { std::stringstream ss; ss.copyfmt(os); // <- re-create formatting ss << rhs._i; homecoming os << ss.str(); }

demo

c++ c++11 stl stream

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -