C + + variable parameter function __jquery

Source: Internet
Author: User

Va_list is a group of macros that solve the problem of variable parameter in C language.
He has so many members:

1) va_list type variable:

#ifdef  _m_alpha
typedef struct {
        char *a0;       /* Pointer to the homed integer argument
        /int offset;     /* byte offset of next parameter *
/} va_list;
#else
typedef char *  va_list;
#endif

2 _intsizeof macros, gets the space length occupied by the type, and takes a minimum length of integral times int:

#define _INTSIZEOF (n)   (sizeof (n) + sizeof (int)-1) & ~ (sizeof (int)-1))

3 Va_start macros, gets the address of the first parameter of the variable argument list (the AP is a pointer of type va_list, and V is the leftmost parameter of the variable parameter):

#define VA_START (AP,V)  (AP = (va_list) &v + _intsizeof (v))

4 VA_ARG the macro, gets the current parameter of the variable parameter, returns the specified type and points the pointer to the next parameter (the T parameter describes the type of the current parameter):

#define VA_ARG (ap,t) (    * (t *) (AP + _intsizeof (t))-_intsizeof (t))

5 va_end macros, empty va_list variable parameter list:

#define VA_END (AP)      (AP = (va_list) 0)

Second, the use of va_list:

(1) First in the function defines a va_list type of variable, which is the pointer to the parameter;
(2) then using the Va_start macro to initialize the variable just defined by the va_list variable;
(3) Then return the variable parameter with Va_arg, and the second parameter of the va_arg is the type of the parameter you want to return (if the function has more than one variable parameter, call Va_arg to fetch each parameter sequentially);
(4) Finally using the Va_end macro to end the acquisition of variable parameters.

Third, the use of va_list should pay attention to the problem:
(1) The type and number of variable parameters are completely controlled by program code, which can not recognize the number and type of different parameters intelligently;
(2) If we do not need one by one detailed each parameter, just copy the variable list to a buffer, available vsprintf function;
(3) because the compiler for the variable parameters of the function of the prototype check is not strict enough for error-checking programming. Bad for us to write high quality code;

Summary: The function principle of variable parameters is actually very simple, and the VA series is defined as a macro definition, and the implementation is related to the stack. We write a variable parameter of the C function, there are advantages and disadvantages, so in unnecessary situations, we do not need to use variable parameters, if in C + +, we should use C + + polymorphism to achieve variable parameter function, try to avoid using the C language method to achieve.

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.