fork download
  1. #include <string>
  2. #include <sstream>
  3. #include <iostream>
  4.  
  5. class Util
  6. {
  7. public:
  8. std::ostringstream m_os;
  9.  
  10. Util() {}
  11. ~Util() { std::cout << m_os.str() << std::endl;}
  12. };
  13.  
  14.  
  15.  
  16. int main (void)
  17. {
  18. // ----------- Using temporary anonymous instance -
  19. // Output does not match expected, and the first insertion seems to
  20. // only be able to handle instances that can be converted to int.
  21.  
  22. // Following prints "97key=val", but expect "akey=val"
  23. (Util()).m_os << char('a') << std::string("key") << "=" << std::string("val");
  24.  
  25. // Following prints "0x80491eakey=val", but expect "Plain old C string key=val"
  26. (Util()).m_os << "Plain old C string " << std::string("key") << "=" << std::string("val");
  27.  
  28. // Following results in syntax error
  29. // error: no match for ‘operator<<’ in ‘Util().Util::m_os <<
  30. (Util()).m_os << std::string("key") << "=" << std::string("val");
  31.  
  32.  
  33. // ----------- Using named instance - output matches expected
  34.  
  35. // Block results in print "akey=val"
  36. {
  37. Util inst;
  38. inst.m_os << char('a') << std::string("key") << "=" << std::string("val");
  39. }
  40.  
  41. // Block results in print "Plain old C string key=val"
  42. {
  43. Util inst;
  44. inst.m_os << "Plain old C string " << std::string("key") << "=" << std::string("val");
  45. }
  46.  
  47. // Block results in print "key=val"
  48. {
  49. Util inst;
  50. inst.m_os << std::string("key") << "=" << std::string("val");
  51. }
  52.  
  53. return 0;
  54. }
Success #stdin #stdout 0s 3432KB
stdin
Standard input is empty
stdout
akey=val
Plain old C string key=val
key=val
akey=val
Plain old C string key=val
key=val