tuple as function argument

假装没事ソ 提交于 2019-12-11 15:25:32

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!