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