fork download
  1. #include <iostream>
  2. #include <cstdarg>
  3.  
  4. using namespace std;
  5.  
  6. template<typename T>
  7. void Print(T val) {
  8. cout << val << endl;
  9. }
  10.  
  11. struct MyStruct {
  12. MyStruct(int a) : b(a) {}
  13. void print() { cout << "MS: " << b << endl; }
  14. int b;
  15. };
  16.  
  17. void Print(MyStruct &val) {
  18. val.print();
  19. }
  20.  
  21. template<typename T>
  22. void Foobar(int nNumberofParams,...) {
  23. va_list args;
  24. va_start(args,nNumberofParams);
  25. for(int i =0 ; i < nNumberofParams; i++)
  26. {
  27. T val = va_arg(args,T);
  28. Print(val);
  29. }
  30. va_end(args);
  31. }
  32.  
  33. int main() {
  34. Foobar<int>(3, 1, 2, 3);
  35. Foobar<MyStruct>(2, MyStruct(1), MyStruct(2));
  36. Foobar<MyStruct>(2, 3, 4);
  37. }
Success #stdin #stdout 0s 2928KB
stdin
Standard input is empty
stdout
1
2
3
MS: 1
MS: 2
MS: 3
MS: 4