fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <utility>
  4. #include <tuple>
  5. #include <functional>
  6.  
  7. #include <vector>
  8.  
  9. using namespace std;
  10.  
  11.  
  12. class Test {
  13. public:
  14. Test() { cout << "Test()" << endl; }
  15. Test(const Test&) { cout << "Test(const Test&)" << endl; }
  16. Test(Test&&) { cout << "Test(Test&&)" << endl; }
  17. ~Test() {}
  18.  
  19. Test& operator = (const Test&) { cout << "copy operator" << endl; }
  20. Test& operator = (Test&&) { cout << "move operator" << endl; }
  21. };
  22.  
  23.  
  24. void OrigFunc(int a, string b, Test t) {
  25. cout << "OrigFunc(" << a << ", " << b << ")" << endl;
  26. }
  27.  
  28.  
  29. template<typename... Args>
  30. void ForwardFunc(Args&&... args) {
  31. OrigFunc(std::forward<Args>(args)...);
  32. //OrigFunc(args...);
  33. }
  34.  
  35.  
  36. template<typename... FuncParams, typename... ArgTypes>
  37. tuple<ArgTypes...> ArgsChecker(function<FuncParams...> func, ArgTypes&&... args) {
  38. tuple<ArgTypes...> arguments(args...);
  39. func(std::forward<ArgTypes>(args)...);
  40. return arguments;
  41. }
  42.  
  43.  
  44. template<int IDX, int NUM_ELEMS>
  45. struct TuplePrinter {
  46. template<typename... TupleArgs>
  47. static void Print(const tuple<TupleArgs...>& hisTuple) {
  48. cout << "tuple[" << IDX << "] : " << get<IDX>(hisTuple) << endl;
  49. TuplePrinter<IDX + 1, NUM_ELEMS>::Print(hisTuple);
  50. }
  51. };
  52.  
  53.  
  54. template<int NUM_ELEMS>
  55. struct TuplePrinter<NUM_ELEMS, NUM_ELEMS> {
  56. template<typename... TupleArgs>
  57. static void Print(const tuple<TupleArgs...>& hisTuple) {
  58. }
  59. };
  60.  
  61.  
  62. int main() {
  63. auto args = ArgsChecker(function<void(int, string, Test)>(OrigFunc), 1234, string("Hello World"), Test());
  64. TuplePrinter<0, tuple_size<decltype(args)>::value - 1>::Print(args);
  65. }
  66.  
Success #stdin #stdout 0s 2988KB
stdin
Standard input is empty
stdout
Test()
Test(const Test&)
Test(Test&&)
Test(Test&&)
Test(Test&&)
OrigFunc(1234, Hello World)
tuple[0] : 1234
tuple[1] : Hello World