C Macro System defects

Source: Internet
Author: User

These two days a little bit of boost preprocessor library, found that the boost of the crazy people to use a variety of artifice to define a variety of data types and structure, but also in the above definition of subtraction and so on various operations, in the process of rapid browsing, but also targeted the very familiar with the various macro names: list, Cons,fold_left, Fold_right, estimates that these people have moved a lot of the features of functional languages. In the spirit of the principle of building wheel skills, I also try to achieve a variety of elements, but not enough IQ, the more difficult to write, and finally died. Summarize briefly, temporarily found that C's macro has the following counter-intuitive shortcomings: 1, can not define local variables, all macros must be defined in the outermost layer, resulting in global visibility and no similar namespace function, the name of the super headache does not support multi-line write, to multiple lines at the end of each line add "\" 2, no control flow, to To realize the loop, choose very troublesome 3, the transfer of the parameters of the mechanism of anti-intuition, normal language of the common use of the application sequence, the first fully expanded parameters, and C in the macro parameter expansion process if you encounter # or # #就停止展开, such as:
1      #define BOOL (n)      bool# #n2      #define BOOL0         03      #define BOOL1         1 4      #define BOOL2         15      #define BOOL3         1

BOOL (n) to get the true and false value of the value n
1 #define IF (c, x, y)       if# #c (x, y)2#define IF0 (x, y)         y3#define IF1 (x, y)         x

The above macro is intended to implement the selection control, if the logical value of C, if C is 0 return y, if C is 1 return x

If called as follows: if (BOOL (3), "T", "F"), by intuition this sentence should be generated "T", can backfire, because the expansion of BOOL (3) encountered # #, so directly return BOOL3, the result of the macro becomes the IF (BOOL3, "T", " F "), pressing if the body continues to expand, then becomes IFBOOL3 (" T "," F ") the last preprocessor error: Cannot find IFBOOL3 therefore, in order to correctly expand the parameter bool (3) to 1, you also need to wrap more than one layer of 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"), the parameters are expanded to become if (BOOL3, "T", "F"),

Then the macro body expands, If_c (BOOL3, "T", "F"), expands the parameters into, If_c (1, "T", "F"), and finally expands correctly to IF1 ("T", "F") 4, missing integer type, which is cumbersome to generate code with counter loops, first You manually define a bunch of whole numbers of 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 the addition, subtraction on the inc_xx and Dec_xx, which is equivalent to the need to manually use the most basic elements to construct the basic method, and then the basic methods are repeatedly nested, abstract higher-order functions, the workload and the creation of language almost originally created language or A very interesting thing, but because of the anti-human anti-intuitive strange transfer mechanism of the existence, resulting in complex methods to construct higher-order functions of the process of abnormal pain, you have to pay attention to the parameter expansion will not be # and # #打断, if interrupted you need to add a layer of macro to continue to expand. 5, can not achieve recursion, such as:
1 #define x y+12      #define y x+1    

then expand X, expand into y+1, continue to expand Y,x+1+1, then hit X again, the preprocessor stops expanding.

Recursion is not possible, and the use of macros for loops becomes unusually verbose. Generally, while loops and tail recursion are equivalent, so if recursion is supported, the loop can be implemented in the form of tail recursion, but it is not supported now, so we need to unfold each step of the tail recursion in person and macro the definition of manual display, such as:
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 recursive depth can only be a fixed value 6, c macro just for simple text substitution, so replace the text may appear, the following is a classic example:
1       #define Square (x)  x*x2       cout<<square (2+3) <<endl;

After replacing it becomes 2+3*2+3, so when you write a macro, you should also pay attention to the necessary place brackets ...

7, there is no way to preach Function-like macro name, such as:
1         #define ADD (n, m) .....   2         #define For (k, op,...)  ......

If you call for ((3,3), add,...) and want to add (3,3) inside for, the preprocessor will error, saying that add is not defined.

That is, the function name can not be passed into the parameters, of course, I found that boost is possible, it is estimated that the use of what artifice, impatient to see, the gods know the words please guide the following.

C Macro System defects

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.