fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. struct clear_line { } cls;
  5.  
  6. class out {
  7. private:
  8. std::ostream &strm = std::cout;
  9. bool is_next_clear = false;
  10. public:
  11. template <typename T>
  12. out& operator<<(const T& obj) {
  13. if(is_next_clear) {
  14. strm << std::endl << std::endl << std::endl; // clear logic
  15. is_next_clear = false;
  16. }
  17.  
  18. strm << obj;
  19. return *this;
  20. }
  21.  
  22. out& operator<<(const clear_line& _) {
  23. is_next_clear = true;
  24. return *this;
  25. }
  26.  
  27. // this is the type of std::cout
  28. typedef std::basic_ostream<char, std::char_traits<char> > CoutType;
  29.  
  30. // this is the function signature of std::endl
  31. typedef CoutType& (*StandardEndLine)(CoutType&);
  32.  
  33. // define an operator<< to take in std::endl
  34. out& operator<<(StandardEndLine manip)
  35. {
  36. // call the function, but we cannot return its value
  37. manip(strm);
  38.  
  39. return *this;
  40. }
  41. };
  42.  
  43. int main() {
  44. out o;
  45. o << "Some real output" << std::endl << "More..." << cls;
  46. o << "Some other real output";
  47.  
  48. return 0;
  49. }
Success #stdin #stdout 0s 4652KB
stdin
Standard input is empty
stdout
Some real output
More...


Some other real output