fork download
  1. #include <iostream>
  2. #include <string>
  3.  
  4. struct foo
  5. {
  6. std::string name;
  7. };
  8.  
  9. struct foowrapper1
  10. {
  11. foo const& obj;
  12. };
  13.  
  14. foowrapper1 print1(foo const& obj)
  15. {
  16. return {obj};
  17. }
  18.  
  19. struct foowrapper2
  20. {
  21. foo const& obj;
  22. };
  23.  
  24. foowrapper2 print2(foo const& obj)
  25. {
  26. return {obj};
  27. }
  28.  
  29. std::ostream& operator<<(std::ostream& lhs, foowrapper1 const& rhs)
  30. {
  31. lhs << '(' << rhs.obj.name << ')';
  32. }
  33.  
  34. std::ostream& operator<<(std::ostream& lhs, foowrapper2 const& rhs)
  35. {
  36. lhs << '[' << rhs.obj.name << ']';
  37. }
  38.  
  39. int main()
  40. {
  41. foo f;
  42. f.name = "blah";
  43.  
  44. std::cout << print1(f) << print2(f);
  45. }
Success #stdin #stdout 0s 3060KB
stdin
Standard input is empty
stdout
(blah)[blah]