Look at the macro (macros) in C/C ++)

Source: Internet
Author: User
Generally, our understanding of C/C ++ macros is nothing more than two points:
1. It is used to process repeated inclusion of header files.
2. Macro used to define similar functions. Everyone can understand the first point. Second, we often see macros used to define similar functions. What is more classic: Define max (A, B) (A> B )? (A) :( B) A similar approach is primarily to bypass type checks, in this way, regardless of the type of A and B, you can use a macro to replace the different versions of the Max function repeatedly. But there is one obvious problem: we lack Type Protection, which is very scary for large applications. (But not all of them ).
Generic method is introduced in C ++ to solve this problem. But what I want to express today is none of the above. The Macros in C/C ++ are also useful: Code Generator ).Maybe you don't care about it. Frankly speaking, I did the same before. Obviously, I knew it when I went to school. For example, in the above example, when you write the following code:
Int IMAX = max (5, 6 );
The compiler will automatically generate code for you: int IMAX = (5> 6 )? (5) (6); indeed, I have understood it for more than three years. Until recently, I was reading Graham Paul's ANSI Common LISP, which has the following sentence in the preface:
Macros, for example: Lisp programmers can, and often do, write programs to write their programs for them. Yes, we can write a program to program for us.
As mentioned above, the abstract level and thinking methods have changed:
Yes, we can write some programs. These programs are used to write programs for us.
Like a tongue twister? If I have some experience, I think this technology/idea is often used in the source code of VC/mfc. At the beginning, I couldn't understand why some macros are always inserted in the source code of MFC, and some macros are huge. At that time, I thought Microsoft's programmers were incredible. Now I know:
For them (the author of MFC), they are sometimes writing programs, but sometimes writing programs (macros) that will help them write programs ).
So it turns out! This article only reminds me not to go into more details. If you are interested, you can:
1. Check the source code of MFC and find out which macros are generic method and code generator.
2. Go to Graham Paul's ANSI Common Lisp.
3. If you are strong enough, you can look at Graham's on lisp; Jerry. Chou

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.