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 ;}