问题
I am a little confused if it possible an how to use a variadic tuple as an argument in a function and how to initialize it.
template <typename T, Arg ...>
void foo (int a, std::tuple<T, sizeof(Arg)> TupleTest);
...
foo(TupleTest(2, "TEST", 5.5));
How could that be implemented using c++0x?
回答1:
You don't need to get the number of template arguments. Just do this:
template <typename... T>
void foo(int a, std::tuple<T...> TupleTest);
// make_tuple so we don't need to enter all the type names
foo(0, std::make_tuple(2, "TEST", 5.5));
回答2:
What do you want sizeof
for? Just use the variadic expansion:
template <typename T, typename Arg ...>
void foo(int a, std::tuple<T, Arg...> TupleTest);
And here, TupleTest
is the name of the argument, not a type name. So when invoking the method, don’t use it.
foo(42, std::tuple<int, char const*, double>(2, "TEST", 5.5));
Finally, the type argument T
serves no real purpose (unless you explicitly want to forbid an empty template list) so you can just remove it without loss.
来源:https://stackoverflow.com/questions/8747406/tuple-as-function-argument