Introduction to C + + templates: What is variable length template function

Source: Internet
Author: User

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 ();

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.