Summary of C + + macro (macro) usage

Source: Internet
Author: User

When we talk about macros in C + +, the first thing we think about is "#define", and its basic syntax looks like this:

1 #define macroname (para1, Para2, Para3, ..., Paran) macro-body

The declaration of a macro is similar to a normal function declaration, but there is an essential difference between the two: C + + functions execute code Snippets at runtime (runtime), while macros execute code snippets at the precompiled time (preprocessor). The following is a brief introduction to several macro applications.

First, consider the following code snippet:

1 #define Plus_one (x) ((x) + 1)2int x=plus_one (N);
>>>x= ((12) + 1)
>>>x=13

This is the simplest application to replace macroname with Macro-body, and unlike the C function, a macro has no return value, and the value of itself expression is returned as a return value.

Second, consider the following code snippet:

1 #define x 1+22int t=x*3; 3 std::cout<<t<<'\ n';

So what is the value of the output T? The answer may not be 9 of what you expect, but 7. The reason is simple, #define的替换实质上是 the substitution of the expression , the macro in the above code will be expanded into this way:

int t = 1 + 2 * 3; t = 7

This result is obviously not what we would like to see, and this error is hard to detect, there is no grammatical error or semantic error, but it is easy to solve the problem by enclosing the expression:

1 #define x (1+2)
>>>int t = (1+2) *3=9

However, this method of using macros is not recommended, because it is easy to cause some unnecessary and imperceptible errors. In C + +, we can declare x in such a way that we can achieve the same goal:

1 Const  int x=3; Declaring constants recommended with the Const keyword

Third, preprocessor pre-defined values;

__FILE__: The absolute path of the compiled file;

__LINE__: Current line number;

__TIME__: Current time;

__DATE__: current date.

In the C language, one of the original design of macro is about the inline function. If we define a max function that asks for a larger value:

1 #define MAX (a) (a) > (b)? ( A):(B))
>>>int Myint=max (x, y)
>>>int myint= ((x) > (y)? ( x):(y))//The macro that is called on the previous line is inserted directly into the code snippet

If we were to write Max as a general function, then we would call Max with the following two procedures: ① calls the function named Max ② returns the value of the comparison. Obviously in both scenarios, the macro scheme is more efficient because the overhead of the omitted function call directly results (equivalent to the use of the inline function, which does not expand the inline).

Five, string manipulation functions

Look at the following macro definitions:

1 #define MAX (a) (a) > (b)? ( A):(B))

The arguments A and B here are actually passed as a string, for example, Max (10,12) returns the string "12" instead of the integer 12. The preprocessor provides two ways to handle parameter passing of type string.

1,stringizing operator ' # ': Returns a C-style string;

1 #define TEST (n) std::cout << #n << "is" << (n) << Std::endl
>>>int x= 6;
>>>test (x*2); Std::cout << "x*2" << "is" << (x*2) << Std::endl

Finally, the result is: X*2 is 12

So what's the use of this seemingly tricky grammar? In fact, when the above-mentioned C + + code is translated into machine code, all the concepts or statements associated with the variable x will be "wiped out", because the variables will only exist in the C + + code layer, and by stringizing operator, let us save a part of the C + + source code as a string becomes possible, This can be used to write some "self-diagnosis (Error correction)" function, there is a chance to introduce in depth!

2. Stringconcatenation operator ' # # ': As the name implies, the string join operator:

1 #define TEST2 (type) type ones_# #type
>>>test2 (int);
>>>int Ones_type; The function of this macro is actually declaring an arbitrary type of variable, such as int, named Ones_type

This doesn't feel any special use, trick.

Summary of C + + macro (macro) usage

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.