Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is std::make_tuple(7 + N...) legal in C++11?

Tags:

The following code is legal in C++11.

template<int... N>
std::tuple<decltype(N)...> f()
{
    return std::make_tuple(7 + N...); 
}

What does it mean?

like image 402
xmllmx Avatar asked Jul 09 '14 07:07

xmllmx


1 Answers

First of all, look at the template parameters: template <int ... N>. Even though a variable number of template arguments can be given to f, all of them must be of type int.

Now when you use f<t1, t2, ..., tn>, the parameter unpacking (7 + N...) will follow the pattern 7 + N and expand to

7 + t1, 7 + t2, 7 + t3, ..., 7 + tn

Therefore you end up with a tuple which contains each of your template arguments increased by seven. The details can be found in section 14.5.3 Variadic templates [temp.variadic].

3. A pack expansion consists of a pattern and an ellipsis, the instantiation of which produces zero or more instantiations of the pattern in a list [...].

like image 82
Zeta Avatar answered Oct 02 '22 19:10

Zeta