Compile-time integer sequences C++14

130 views
Skip to first unread message

Piet

unread,
May 13, 2014, 3:52:56 PM5/13/14
to

hello,

20.5.1 of the C++14 draft reads

example:
template<class F, class Tuple, std::size_t... I>
decltype(auto) apply_impl(F&& f, Tuple&& t, index_sequence<I...>) {
return std::forward<F>(f)(std::get<I>(std::forward<Tuple>(t))...);
}

What I do not understand:
The I in get<I> is declared as parameter pack in the template
parameter list, but should be a number or a type of one of the tuple
elements. Am I wrong?

How f could be declared (see the ellipsis in the call)?

Best regards
Piet






--
[ comp.std.c++ is moderated. To submit articles, try posting with your ]
[ newsreader. If that fails, use mailto:std-cpp...@vandevoorde.com ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]

Daniel Krügler

unread,
May 14, 2014, 11:19:36 PM5/14/14
to

Am 13.05.2014 21:52, schrieb Piet:
>
> 20.5.1 of the C++14 draft reads
>
> example:
> template<class F, class Tuple, std::size_t... I>
> decltype(auto) apply_impl(F&& f, Tuple&& t, index_sequence<I...>) {
> return std::forward<F>(f)(std::get<I>(std::forward<Tuple>(t))...);
> }
>
> What I do not understand:
> The I in get<I> is declared as parameter pack in the template
> parameter list, but should be a number or a type of one of the tuple
> elements. Am I wrong?


In this declaration I is a non-type template parameter pack whose
expansion would result in a sequence of N std::size_t values I1, I2,
..IN. And the free std::get overload for tuples (such as std::tuple)
expects a single std::size_t template argument for its first template
parameter. The expansion of

std::forward<F>(f)(std::get<I>(std::forward<Tuple>(t))...)

would therefore result in the falling call expression:

std::forward<F>(f)(std::get<I1>(std::forward<Tuple>(t)),
std::get<I2>(std::forward<Tuple>(t)),
...,
std::get<IN>(std::forward<Tuple>(t)))

Does this answer your question?

> How f could be declared (see the ellipsis in the call)?


f can be any function object type whose function call operator can be
invoked with the N elements of a given N-tuple.

Lets say the tuple is std::tuple<int, float>, the function object f
could be a type such as

struct MyF
{
void operator()(int, double);
};

or it could be a function pointer such as the followoing one

int (*)(long, float);

just to name some obvious examples.

HTH & Greetings from Bremen,

Daniel Krügler
Reply all
Reply to author
Forward
0 new messages