fork download
  1. #include <iostream>
  2. #include <fstream>
  3.  
  4. using namespace std;
  5.  
  6. class LoggedStream {
  7. private:
  8. ostream& out;
  9. public:
  10. LoggedStream(ostream& o):out(o){}
  11. template<typename T>
  12. const LoggedStream& operator<<(const T& v) const {
  13. out << v;
  14. return *this;
  15. }
  16. LoggedStream const& operator<<(std::ostream& (*func)(std::ostream&)) const {
  17. func(out);
  18. return *this;
  19. }
  20. };
  21.  
  22. int main(int,char**) {
  23. LoggedStream Log1(std::cout);
  24. Log1 << 1 << " 2" << endl << 3 << " 4" << endl;
  25. //
  26. std::ofstream ofs("test.txt", std::ofstream::out);
  27. LoggedStream Log2(ofs);
  28. Log2 << 1 << " 2" << endl << 3 << " 4" << endl;
  29. }
Success #stdin #stdout 0s 4544KB
stdin
Standard input is empty
stdout
1 2
3 4