fork(3) download
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. namespace manips
  5. {
  6. template <typename Cont, typename Delim=const char*>
  7. struct PrintManip {
  8. PrintManip(Cont const& v, Delim d = ", ") : _v(v), _d(std::move(d)) { }
  9.  
  10. Cont const& _v;
  11. Delim _d;
  12.  
  13. friend std::ostream& operator<<(std::ostream& os, PrintManip const& manip) {
  14. using namespace std;
  15. auto f = begin(manip._v), l(end(manip._v));
  16.  
  17. os << "{ ";
  18. while (f != l)
  19. if ((os << *f) && (++f != l))
  20. os << manip._d;
  21. return os << " }";
  22. }
  23. };
  24.  
  25. template <typename T, typename Delim=const char*>
  26. manips::PrintManip<T, Delim> print(T const& deduce, Delim delim = ", ") {
  27. return { deduce, std::move(delim) };
  28. }
  29. }
  30.  
  31. using manips::print;
  32.  
  33. int main()
  34. {
  35. const char* arr[] = { "hello", "bye" };
  36. std::cout
  37. << "Woot, I can has " << print(arr)
  38. << " and even: " << print(std::vector<int> { 1,2,3,42 }, ':') << "!\n"
  39. << "or bizarrely: " << print(arr, print(arr)) << "\n";
  40. }
  41.  
Success #stdin #stdout 0s 2988KB
stdin
Standard input is empty
stdout
Woot, I can has { hello, bye } and even: { 1:2:3:42 }!
or bizarrely: { hello{ hello, bye }bye }