C macro system defects, c macro Defects

Source: Internet
Author: User

C macro system defects, c macro Defects
I took a look at the boost preprocessor library a little over the past two days. I found that the boost madmen used various tricks to define various data types and structures, and also defined various operations such as addition, subtraction, multiplication, division, and so on, in the process of quick browsing, we also caught a variety of well-known macro names: list, cons, fold_left, and fold_right. It is estimated that these guys have also moved many features of functional languages. I also tried to implement various elements on my own based on the principle of building on the wheel and practicing my skills. However, my IQ is not enough. The more I write, the more uncomfortable it is. After a rough summary, we found that the macro of C has the following Disadvantages: 1. Local variables cannot be defined, and all macros must be defined at the outermost layer, as a result, it is globally visible and does not have a namespace-like function. It is a headache During naming and does not support writing multiple rows. To add multiple rows to the end of each row, "\" 2. No control flow, it is difficult to implement loops and select parameters. 3. The parameter passing mechanism is not intuitive. Parameters passing in normal languages are generally in the application order. parameters are completely expanded before being passed in, if # Or # Is encountered during macro Parameter Expansion of C, stop expansion, for example:

1      #define BOOL(n)      BOOL##n2      #define BOOL0         03      #define BOOL1         14      #define BOOL2         15      #define BOOL3         1

 

BOOL (n) can obtain the true value of n.
1 #define IF(c, x, y)       IF##c(x, y)2 #define IF0(x, y)         y3 #define IF1(x, y)         x

The macro above is to implement selection control. IF the logic value c is input, IF c is 0, return y. IF c is 1, return x.

IF you call it as follows: IF (BOOL (3), "t", "f"), generate "t" based on intuition. This can be counterproductive because expand BOOL (3) IF # Is encountered, then BOOL3 is returned directly, and the macro above the result becomes IF (BOOL3, "t", "f"), and continues to expand according to the IF macro, IFBOOL3 ("t", "f") is changed to the final pre-processor error: IFBOOL3 cannot be found. Therefore, in order to correctly expand the BOOL (3) parameter to 1, you also need to package more macros:
1 #define IF(c, x, y)       IF_C(c, x, y)   2 #define IF_C(c, x, y)   IF##c(x, y)

In this way, IF (BOOL (3), "t", "f") expands the parameter and changes it to IF (BOOL3, "t", "f "),

Then expand the macro, IF_C (BOOL3, "t", "f"), expand the parameter to, IF_C (1, "t", "f "), finally, it will be correctly expanded to IF1 ("t", "f") 4. The integer type is missing. It is very troublesome to generate code using counter loops, first, you must manually define a bunch of integers INC:
1       #define INC_0 12         #define INC_1 23         #define INC_2 34         #define INC_3 45         ……………6  7         #define DEC_x x8         ………………

 

Then, we define addition and subtraction on INC_xx and DEC_xx. This is equivalent to manually constructing basic methods using the most basic elements, and then composite and nesting these basic methods continuously, abstract higher-order functions. The workload is similar to that of language creation. It is quite interesting to create a language. However, due to the existence of the anti-human and anti-intuitive odd parameter passing mechanism just mentioned, as a result, the process of constructing a High-Order Function in the composite method is very painful. From time to time, you have to pay attention to whether the parameters are interrupted by # And #. If the parameters are interrupted, you need to add a macro to continue expansion. 5. recursion is not possible, for example:
1    #define x y+12      #define y x+1

Then, expand x to y + 1, continue to expand y, x + 1 + 1, and then run into x, the pre-processor stops expanding.

If recursion is not possible, it will become unusually lengthy when macro-current loops are used. In general, the while loop and tail recursion are equivalent. If recursion is supported, the loop can be implemented in the form of tail recursion, but it is not supported now. Therefore, we need to expand each step of tail recursion in person, and define the manually displayed macro as follows:
1         #define WHLE(...)       WHILE##n(...)2         #define WHILE0(...)     xxxxx3         #define WHLE1(....)     WHILE0(......)4         #define WHLE2(....)     WHILE1(......)5         #define WHLE3(....)     WHILE2(......)6         ...................  

 

This is not only troublesome, but also the recursive depth can only be a fixed value 6. The macro of c is only used for simple text replacement. Therefore, it may appear after being replaced with the text, the following is a classic example:
1       #define square(x)  x*x2       cout<<square(2+3)<<endl;

After replacement, it becomes 2 + 3*2 + 3. Therefore, when writing a macro, you must add brackets where necessary .........

7. function-like macro name cannot be passed, for example:
1         #define ADD(n, m)   ..........2         #define FOR(k, op,...)  ......

If you call FOR (3, 3), ADD,...) and want to ADD (3, 3) in the for, the pre-processor reports an error, saying that ADD is not defined.

That is to say, the function name cannot be passed in as a parameter. Of course, I found that the boost function can be used. It is estimated that it is so cool and cool. If you know it, please give us the following instructions.

Related Article

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.