C ++ macro programming and macro Programming

Source: Internet
Author: User

C ++ macro programming and macro Programming
1. Define a macro function so that func (x) = X-1? In the simplest way, if x = 2, # define func (x) 1 is specially converted to x, so that # define func_helper_2 1 # define func (x) func_helper _ # x is created when x = 2. to promote to other numbers, you only need to define: # define func_helper_2 1 # define func_helper_3 2 # define func_helper_4 3... then we can get the decreasing and increasing functions # define DEC (X) # define INC (X)
2. How many macro variable parameters can be obtained? Take a look at this situation # define func (a1, a2, a3, a4,...) a4 what will happen if func (1, 2, 3, 4, 5) is passed in? Of course it is 4 to pass in func (x, 1, 2, 3, 4, 5) at this time? Is 3 passed to func (x, x, 1, 2, 3, 4, 5? Is 2 passed to func (x, 5? Is it 1? Is 1, 2, 3, so # define func2 (...) func (_ VA_ARGES _).
Finally, GET_ELEM_SIZE (X) is used to obtain the number of parameters in the 5-digit range.
3. Obtain the nth parameter? For example, GET_ELEMENT (a1, a2, a3), 2) expected to get a3?
# Define GET_ELEMENT (array, n) GET_ELEMENT _ # n arrayGET_ELEMENT_0 (e0,...) equals (e0, e1. ..) e1GET_ELEMENT_0 (e0, e1, e2...) e2
In this way, you can obtain the parameters of the specified coordinate position within three numbers.
4, int to BOOL? # Define INT_TO_BOOL (X) INT_TO_BOOL _ # X # define limit 0 # define limit 1 # define INT_TO_BOOL_2 1 # define INT_TO_BOOL_3 1 # define INT_TO_BOOL_4 1
5. IF branch? # Define IF_TEST (condition, true_verb, false_verb) IF_TEST _ # condition (true_verb, condition) \ # define condition (true_verb, false_verb) false_verb # define condition (true_verb, false_verb) true_verb
3, iterator? If ITER_FUNC (n, context) is defined as a macro function with n as the parameter, then: # define ITERATOR (n, ITER_FUNC, context) ITERATOR _ # n (ITER_FUNC, context) # define functions (n, func) # define ITERATOR_1 (func, context) ITERATOR_0 (func, context) func (1, context) # define ITERATOR_2 (func, context) ITERATOR_1 (func, context) func (2, context) # define ITERATOR_3 (func, context) ITERATOR_2 (func, context) func (3, context) reverse version # define ITERATOR_REVERSE (n, ITER_FUNC, context) ITERATOR_REVERSE _ # n (ITER_FUNC, context) # define functions (n, func) # define ITERATOR_REVERSE_1 (func, context) func (1, context) functions (func, context) # define functions (func, context) func (2, context) ITERATOR_REVERSE_1 (func, context) # define functions (func, context) func (3, context) ITERATOR_REVERSE_2 (func, context)
4. Data Structure? Based on the preceding tools, you can define the macro array structure. The array format can be defined as: array = (ele0, ele1, ele2) to obtain the array size, which can be used in Case 2: # define GET_ARRAY_SIZE (array) GET_ELEM_SIZE array accesses the array subscript to obtain the element: # define GET_ARRAY_ELEMENT (array, n) GET_ELEMENT (array, n) pushes an element: # define EXPAND_PARAM (param) param # define PUSH_ARRAY_ELEM (array, elem) EXPAND_PARAM array IF_TEST (GET_ARRAY_SIZE (array), EMPTY (), COMMA () elempop one element: # define POP_ARRAY_ELEM (array) ITERATOR (DEC (GET_ARRAY_SIZE (array), COPY_ARRAY_FROM, array) # define COPY_ARRAY_FROM (n, array) IF_TEST (n, EMPTY (), COMMA () GET_ARRAY_ELEMENT (array, n)

For example! What I catch up with is the implementation of c ++ tuple. Do you still remember the many tuple parameters? Review: \ file tuple. htemplate <> class tuple {}
Template <class T1> class tuple {tuple (T1 a1 ){}};
Template <class T1, class T2> class tuple {tuple (T1 a1, T2 ){}};
10 parameters are required to support 10 parameters...
Use macro iteration functions:
ITERATOR (2, GEN_TUPLE, pass)
# Define GEN_TUPLE (n, pass) \ template <ITERATOR (n, pass, GEN_TPARAM)> \ class tuple {\ tuple (ITERATOR (n, class T, GEN_PARAM )) {}\};
# Define GEN_TPARAM (n, pass) IF_TEST (n, EMPTY (), COMMA () class T # n # define GEN_PARAM (n, pass) IF_TEST (n, EMPTY (), COMMA () T # n a # n

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.