fork download
  1. #include <ctime>
  2. #include <iostream>
  3. #include <sstream>
  4. #include <string>
  5. #include <iomanip>
  6. #include <utility>
  7.  
  8. struct make_string
  9. {
  10. template <typename T>
  11. make_string & operator<<(T && t)
  12. {
  13. ostr << std::forward<T>(t);
  14. return *this;
  15. }
  16.  
  17. operator std::string() const { return ostr.str(); }
  18.  
  19. std::string str() const { return ostr.str(); }
  20.  
  21. std::ostringstream ostr;
  22. };
  23.  
  24.  
  25. void message(const std::string&) {}
  26.  
  27. int main()
  28. {
  29. message(make_string() << "1 + 1 = " << 2 << "\n"); // OK
  30. message(make_string() << "1 + 1 = " << 2 << std::endl); // Not OK
  31.  
  32.  
  33. // OK
  34. std::ostringstream os;
  35. os << std::endl;
  36.  
  37. // Not ok.
  38. make_string mk;
  39. mk << std::endl;
  40. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In function 'int main()':
prog.cpp:30:54: error: no match for 'operator<<' in '((make_string*)make_string().make_string::operator<< [with T = const char (&)[9], make_string = make_string](((const char (&)[9])"1 + 1 = ")))->make_string::operator<< [with T = int, make_string = make_string](((int&&)(&2))) << std::endl'
prog.cpp:39:16: error: no match for 'operator<<' in 'mk << std::endl'
stdout
Standard output is empty