fork download
  1. #include <iostream>
  2. #include <string>
  3.  
  4. struct counter {
  5. size_t ints, doubles, strings, others;
  6.  
  7. counter(): ints(0), doubles(0), strings(0), others(0) {}
  8.  
  9. counter & operator , (int) { ++ints; return *this; }
  10. counter & operator , (double) { ++doubles; return *this; }
  11.  
  12. counter & operator , (char const *) { ++strings; return *this; }
  13. counter & operator , (std::string const &) { ++strings; return *this; }
  14.  
  15. template <class T>
  16. counter & operator , (T const &) { ++others; return *this; }
  17.  
  18. template <class T>
  19. counter & operator += (T const & v) { return this->operator , (v); }
  20.  
  21. friend std::ostream & operator << (std::ostream & o, counter const & c)
  22. {
  23. return o << "counter{ ints:" << c.ints << ", doubles:" << c.doubles << ", strings:" << c.strings
  24. << ", others:" << c.others << " }";
  25. }
  26. };
  27.  
  28. int main()
  29. {
  30. counter c;
  31. c += 10, 10., 200, "hello!", true, 5, 'x';
  32. std::cout << c << std::endl;
  33.  
  34. return 0;
  35. }
Success #stdin #stdout 0s 2884KB
stdin
Standard input is empty
stdout
counter{ ints:3, doubles:1, strings:1, others:2 }