C + + uses macros to calculate the number of variable parameters at compile time

Source: Internet
Author: User

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

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.