How do I expand a tuple into variadic template fun

2018-12-31 08:57发布

Consider the case of a templated function with variadic template arguments:

template<typename Tret, typename... T> Tret func(const T&... t);

Now, I have a tuple t of values. How do I call func() using the tuple values as arguments? I've read about the bind() function object, with call() function, and also the apply() function in different some now-obsolete documents. The GNU GCC 4.4 implementation seems to have a call() function in the bind() class, but there is very little documentation on the subject.

Some people suggest hand-written recursive hacks, but the true value of variadic template arguments is to be able to use them in cases like above.

Does anyone have a solution to is, or hint on where to read about it?

13条回答
梦该遗忘
2楼-- · 2018-12-31 09:58

The news does not look good.

Having read over the just-released draft standard, I'm not seeing a built-in solution to this, which does seem odd.

The best place to ask about such things (if you haven't already) is comp.lang.c++.moderated, because some folks involved in drafting the standard post there regularly.

If you check out this thread, someone has the same question (maybe it's you, in which case you're going to find this whole answer a little frustrating!), and a few butt-ugly implementations are suggested.

I just wondered if it would be simpler to make the function accept a tuple, as the conversion that way is easier. But this implies that all functions should accept tuples as arguments, for maximum flexibility, and so that just demonstrates the strangeness of not providing a built-in expansion of tuple to function argument pack.

Update: the link above doesn't work - try pasting this:

http://groups.google.com/group/comp.lang.c++.moderated/browse_thread/thread/750fa3815cdaac45/d8dc09e34bbb9661?lnk=gst&q=tuple+variadic#d8dc09e34bbb9661

查看更多
登录 后发表回答