fork download
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. template<typename T>
  6. struct Params : public std::vector<T>
  7. {
  8. template<typename V> // avoids fallback to language-defined behavior
  9. inline Params<T>& operator,(const V& value)
  10. {
  11. this->push_back(value);
  12. return *this;
  13. }
  14. };
  15.  
  16. void printParams(const Params<int>& params = Params<int>())
  17. {
  18. cout << "[";
  19.  
  20. for (size_t i = 0; i < params.size(); ++i)
  21. {
  22. cout << (i == 0 ? "" : ", ");
  23. cout << params[i];
  24. }
  25.  
  26. cout << "]" << endl;
  27. }
  28.  
  29. int main()
  30. {
  31. // No params
  32. printParams(); // []
  33.  
  34. // No params with explicit empty params
  35. printParams(Params<int>()); // []
  36.  
  37. // Some params
  38. printParams((Params<int>(), 1, 2, 3)); // [1, 2, 3]
  39.  
  40. // compile-time error: invalid conversion from `const char[4]` to `int`
  41. // printParams((Params<int>(), 1, 2, 3, "error"));
  42. }
Success #stdin #stdout 0s 4292KB
stdin
Standard input is empty
stdout
[]
[]
[1, 2, 3]