SRD

vary

SRD
Mar 8th, 2016
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.95 KB | None | 0 0
  1. I would like to implement a call that takes a variable number of arguments (up to 7). So trying to implement a variadic template function which I call like:
  2.  
  3. foo(1, 2, "msg", "msg2", 5, 6.0);
  4. or
  5. foo(3, "msg", 3.0);
  6.  
  7. The first parameter identifies a protocol to use, and every parameter thereafter is what's included in the protocol.
  8.  
  9. struct Msg {
  10. int proto;
  11. string a;
  12. string b;
  13. int c;
  14. double d;
  15. };
  16.  
  17. template<typename T>
  18. T bar(T t) {
  19. cout << __PRETTY_FUNCTION__ << endl;
  20. return t;
  21. }
  22.  
  23. template<typename T, typename... Args>
  24. void foo(T value, Args... args)
  25. {
  26. cout << __PRETTY_FUNCTION__ << endl;
  27. Msg msg;
  28. msg.proto = value;
  29.  
  30. switch (value) {
  31. case 1:
  32. // when calling 'foo(1, 2, "msg", "msg2", 5, 6.0)'
  33. // 1 is proto, already saved above
  34. // but how to get the remaining params from args into struct Msg
  35. foo(args...);
  36. break;
  37.  
  38. case 3:
  39. foo(args...);
  40. break;
  41.  
  42. default:
  43. break;
  44. }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment