Variable Parameter template added to the C ++ 11 standard)

Source: Internet
Author: User

C ++ 11Variable Parameter template added to the standard"(Variadic template).

In a variable parameter templateTypenameThe number is variable length. The following is an example.G ++ 4.6.1And runs successfully.

 /*  * C ++ 11 standard variable parameter template sample *** copyright ye Jianfei 2012 *** compilation command: * g ++ myprintf. CPP-O myprintf-STD = C ++ 0x-wall *  */  # Include <Iostream> # Include <Cstdlib> # Include <Stdexcept>Void Myprintf ( Const   Char * S ){  While (* S ){  If (* S = '  %  '  ){  If (* (S + 1 ) = '  % '  ){ ++ S ;}  Else  {  Throw STD: runtime_error ( "  Invalid format string: Missing arguments  "  ) ;}} STD: cout <* S ++ ;}}  Template < Typename T,Typename ... ARGs> Void Myprintf ( Const   Char * S, T value, argS... ARGs ){  While (* S ){  If (* S = '  %  '  ){  If (* (S + 1 ) ='  %  '  ){ ++ S ;}  Else  {STD: cout < Value; myprintf (S + 1 , ArgS ...); //  It is called even when * s = 0 to detect unnecessary parameters.                  Return  ;} STD: cout <* S ++;}  Throw STD: logic_error ( "  Extra arguments provided to myprintf  "  );}  Int  Main (){  //  Output a parameter for each percent sign Myprintf ( "  A % bcde % fghij % KL % Mn \ n  " , 12 , " Interesting  " , 8421 , "  Very_interesing  "  );  Return  Exit_success ;} 

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.