The recent C + + language standard has some further complex features, such as adding a variable-length template. In the process of trying to understand this feature,
One of the biggest problems with this is that there are not enough simple code examples to explain how the variable length template is used and works.
Here's one of my basic examples to illustrate variable length templates:
Template <class ... a> int func (A ... arg)
{
return sizeof ... (ARG);
}
int main (void)
{
return func (1,2,3,4,5,6);
}
The first introduction is some terminology: a template parameter can now be a template parameter package, <class ... A>. A template parameter package can represent any number of
Template parameters. In the example above, the template <class ... A> defines the Func function, which has any number of function arguments. The function argument (A.. Arg) is
A function parameter package, he represented the template template <class with a "form" of a parameter ... A> each member of the parameter package. In this example, we used 6 parameters to invoke the function func.
The template parameter derivation (template argument deduction) <class The parameter package ... A> derivation becomes
<int,int,int,int,int,int>, then the function parameter package becomes (int,int,int,int,int,int), exactly corresponds to 6 pass over the shaping parameter.
Variable-length operator sizeof ... Simply returns the number of parameters (function or template) of the parameter package, with a result of 6.
Of course, any number of parameter packages can be empty, consider the following code example:
Template <class ... a> int func (A ... arg)
{
return sizeof ... (ARG);
}
int main (void)
{
return func ();
}