fork download
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <ctime>
  4. #include <string>
  5. #include <iterator>
  6. #include <vector>
  7.  
  8. template < typename STREAM_TYPE, STREAM_TYPE& stm >
  9. struct basic_vstream
  10. {
  11. template< typename T >
  12. static STREAM_TYPE& print_any(const T& v) { return stm << v; }
  13.  
  14. static STREAM_TYPE& print_any(const basic_vstream&) { return stm; }
  15.  
  16. template< typename FIRST, typename... REST >
  17. static STREAM_TYPE& print_any(const FIRST& first, const REST&... rest)
  18. {
  19. const std::size_t n = sizeof...(rest) +1;
  20. if (std::rand() % n == 0) return stm << first;
  21. else return print_any(rest...);
  22. }
  23.  
  24. template < typename... ARGS >
  25. const basic_vstream& operator() (const ARGS&... args) const
  26. {
  27. print_any(args...);
  28. return *this;
  29. }
  30.  
  31. // etc
  32.  
  33. const basic_vstream& operator() (STREAM_TYPE& (*manip)(STREAM_TYPE&)) const
  34. {
  35. stm << manip; return *this;
  36. }
  37.  
  38.  
  39. // other manipulators
  40. };
  41.  
  42. using vstream = basic_vstream< std::ostream, std::cout >;
  43. using wvstream = basic_vstream< std::wostream, std::wcout >;
  44. const vstream vout;
  45. const wvstream wvout;
  46.  
  47. int main()
  48. {
  49. std::srand(std::time(nullptr));
  50. const char space = ' ';
  51.  
  52. for (int i = 0; i < 10; ++i)
  53. vout("Attention!", "Hear this!", "Alert!") (space) ("We must leave!", "We're out of money!") ('.') (std::endl);
  54.  
  55. std::cout << '\n';
  56.  
  57. for (int i = 0; i < 10; ++i)
  58. {
  59. vout("Attention!", "Hear this!", "Alert!") (space)
  60. ("We must leave!", "We're out of money!",
  61. vout("I hate", "We detest", "I loathe") (space)
  62. ("you", "them", "these guys", "those people")('.', '!')) (std::endl);
  63. }
  64.  
  65. std::cin.get();
  66. }
Success #stdin #stdout 0s 3304KB
stdin
Standard input is empty
stdout
Alert! We're out of money!.
Hear this! We must leave!.
Alert! We're out of money!.
Alert! We're out of money!.
Attention! We're out of money!.
Hear this! We must leave!.
Attention! We must leave!.
Alert! We must leave!.
Alert! We're out of money!.
Attention! We're out of money!.

I hate them.Attention! We're out of money!
I loathe you!Alert! We must leave!
I hate them!Alert! We're out of money!
I hate you!Attention! 
We detest those people!Attention! We're out of money!
I loathe them.Alert! We're out of money!
We detest those people.Attention! We're out of money!
I loathe these guys!Hear this! We're out of money!
I loathe these guys.Attention! 
I loathe those people!Attention! We must leave!