c++11 template for variable parameters

Source: Internet
Author: User

The argument template that appears in c++11 lets us not care about the parameters of the function call, similar to implementing the printf function in C.

The arguments depend on the powerful templates of C + +

Can declare this

Template<class T1,class ... Args>//args is a type parameter package that requires recursive parsing in defined functions

void Mutiarg (const T1&t1,args ... Args)

{

Do something with T1 operation on the first invocation parameter

Mutiarg (args ...); The parameter packet is recursively parsed, and the function is called until its number of arguments is 1 o'clock stop call
}

Call the function below when the number of arguments is resolved to only one

Template<class t1>

void Mutiarg (const T1&T1)

{

Do something with T1
}

A complete example of testing directly below

#incldue <iostream>

#include <string>

Template<class T1, class ... Args>
void Mutiarg (const t1&t1, args ... args)
{
Std::cout << T1 << Std::endl;
Mutiarg (args ...);
}

Template<class t>
void Mutiarg (const t& T)
{
Std::cout << "t=" << t << Std::endl;
Std::cout << "End" << Std::endl;
}

void Main ()

{

Mutiarg (1,1.2,3, "string", ' C ');
}

The result of the operation is

1

1.2

3

String

C

--------------End

c++11 template for variable parameters

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.