Reference https://groups.google.com/forum/#!forum/comp.std.c/d-6Mj5Lko_s
Cppformat source code is also referenced.
C + + is not advocating variable parameters, the reason is not said. But the function families like printf are really handy to use, and I was inadvertently looking at the alternatives to printf. Under the emotion, Microsoft's Cstring::format really is very useful
Cppformat provides printf-like functionality, but is type-safe, with the core of using C + + 's powerful template capabilities. In the implementation, Cppformat implements the calculation of the number of variable parameters at compile time, the principle is https://groups.google.com/forum/#!forum/comp.std.c/d-6Mj5Lko_s inside the discussion
For the sake of brevity, only a maximum of three variable parameters are discussed here.
#define arg_n (a1,a2,a3,n,...) N
The function of this macro is to intercept and return the fourth parameter of the argument list
#define arg_n_helper (...) Arg_n (__va_args__)
Auxiliary macros, to make the results calculated correctly
#define count_arg (...) Arg_n_helper (__va_args__,3,2,1,0)
Calculate the number of variable parameters of the macro, the number of variable parameters is greater than 0, less than or equal to 3 within the range calculated correctly
For example:
1count_arg (x, y), 2count_arg (count_arg) , 3 with Count_arg Schematic calculation process Count_arg (x, y), Arg_n_helper (x,y,3,2,1,0), Arg_n (x,y,3,2,1,0) intercept fourth parameter 2
Also, on the VC compiler, compilation may fail to define the following macro
#define arg_t (T) t#define arg_n_helper (...) arg_t (Arg_n (__va_args__))
The complete macro is as follows
#define ARG_T (t) t //Resolve VC Compilation error # define ARG_N (A1,a2,a3,n,...) N //Intercept and return a fourth parameter, which limits the range of variable parameter calculations [1,3] #define ARG_N_HELPER (...) arg_t (Arg_n (__va_args__)) //Auxiliary macro # define COUNT_ARG (...) Arg_n_helper (__va_args__,3,2,1,0) //return variable number of parameters
Schematic code
int main () { int argn = Count_arg (n/a);//argn value is 3 return 0;}
C + + uses macros to calculate the number of variable parameters at compile time