fork download
  1. #include <ios>
  2.  
  3. struct A {};
  4.  
  5. int getFormatIPIndex()
  6. {
  7. static int const i = std::ios_base::xalloc();
  8. return i;
  9. }
  10.  
  11. enum flag_t { Hello, World };
  12.  
  13. struct setflag
  14. {
  15. flag_t flag;
  16.  
  17. setflag( flag_t f ) :
  18. flag{f} {}
  19. };
  20.  
  21. #include <ostream>
  22.  
  23. std::ostream& operator<<( std::ostream& os, setflag a )
  24. {
  25. os.iword(getFormatIPIndex()) = a.flag;
  26. return os;
  27. }
  28.  
  29. std::ostream& operator<<( std::ostream& os, A )
  30. {
  31. auto index = os.iword(getFormatIPIndex());
  32.  
  33. if( index == Hello )
  34. os << "Hello ";
  35. else if( index == World )
  36. os << "World";
  37.  
  38. return os;
  39. }
  40.  
  41. #include <iostream>
  42.  
  43. int main()
  44. {
  45. std::cout << setflag(Hello) << A{} << setflag(World) << A{};
  46. }
  47.  
Success #stdin #stdout 0s 3340KB
stdin
Standard input is empty
stdout
Hello World