Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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:
- foo(1, 2, "msg", "msg2", 5, 6.0);
- or
- foo(3, "msg", 3.0);
- The first parameter identifies a protocol to use, and every parameter thereafter is what's included in the protocol.
- struct Msg {
- int proto;
- string a;
- string b;
- int c;
- double d;
- };
- template<typename T>
- T bar(T t) {
- cout << __PRETTY_FUNCTION__ << endl;
- return t;
- }
- template<typename T, typename... Args>
- void foo(T value, Args... args)
- {
- cout << __PRETTY_FUNCTION__ << endl;
- Msg msg;
- msg.proto = value;
- switch (value) {
- case 1:
- // when calling 'foo(1, 2, "msg", "msg2", 5, 6.0)'
- // 1 is proto, already saved above
- // but how to get the remaining params from args into struct Msg
- foo(args...);
- break;
- case 3:
- foo(args...);
- break;
- default:
- break;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment