Analysis of Macros in C ++

Source: Internet
Author: User

Speaking of macros, I'm afraid everyone can say something: A preprocessing without a semicolon (is it true ?). What then?
Well ......
Okay, let's start from here.
The most common macro may be # include, followed by # define and .......
Let's classify the macro's usage:

1. # include is mainly used to include referenced files, so far its position can be replaced by no one;

2. comment out the code. For example:
# If 0
.......
# Endif;
This mechanism is currently the best option to comment out the code, which is widely used by Motorola employees;

3. Code version management. For example:
# Ifdef debug
File: // debug version
# Else
File: // non-debug version
# Endif;

4. shengminghong. For example:
# Define declare_message (x) x ();~ X () file: // is there a semicolon? Haha
//........
Class
{
Public:
Declare_message ();
..............
}
I think of something, haha :) yes, there are many such things in VC. I will write "My VC history" When I have time,
The various Macros in VC will be explained in detail, which is a huge project :)

5. Symbolic constants. For example:
# Define PI 3.14159

6. inline functions. For example:
# Define clear (x) = 0)

7. generic functions. For example:
# Define ABS (x)> 0? (X):-(x ))
X = 3 No problem! X = 1.3 OK!
If so:
# Include <iostream. h>
# Define a (x)> 0? (X):-(x ))

Void main ()
{
Int I =-1;
Cout <A (1) <Endl;
Cout <A (++ I) <Endl;
}
There is a problem, but later I will talk about what const or Inline will say :)

8. generic type. For example:
# Define stack (t) stack _ # T
# Define stackdeclare (t) class Stack (t ){.....}
Stackdeclare (INT );
Stackdeclare (char );
.......
Stack (INT) S1;
Stack (char) S2;

9. Syntax extension. For example:
Set <int> S; // assume that set is a class that describes the set.
Int I;
Forall (I, S );
.......

The biggest problem with macros is that they are prone to conflicts, for example:
LIBA. h:
# Define macro stuff
At the same time:
Libb. h:
# Define macro stuff
We will reference them below:
User. cpp:
# Include "LIBA. H"
# Include "libb. H"
.............
Bad. There is a redefinition!
There is another possibility of conflict:
Libb. h: (no definition macro)
Class X {void macro ();...........};
During the program running, the macro in LIBA. H will change the name of the member function in libb. H, leading to unpredictable
.
Another problem with macros is that if you set X in 7 to 'A', the program will not
He warned, so he is not safe.
To address the above problems, we will say:
1. Use as few public macros as possible. replace them if possible;
2. Use naming conventions for macros that cannot be replaced;

1. We can use const or enum to replace the symbolic constant Preprocessing Program:
Const int tablesize = 1024;
Enum {tablesize = 1024 };

2. Non-generic inline functions can be replaced by true inline functions:
Inline void clear (Int & X) {x = 0 ;}
Yes, there is another situation:
# Define control (C) (c)-64)
..........
Switch (c)
{
Case Control ('A '):......
Case Control ('B '):......
Case Control ('C '):......
Case Control ('D '):......
..........
}
At this time, the inline function cannot be used alone, because the case tag prohibits function calls, so we have
Perform the following conversions:
Inline char control (char c) {return C + 64 ;}
...........
Switch (control (C ))
{
Case 'A ':.....
Case 'B ':.....
Case 'C ':.....
Case 'D ':.....
........
}
Of course, this is done at the cost of time (you can think about why :))

3. For generic preprocessing programs, we can use a function template or a class mock board instead:
Template <class T>
T ABS (const T & T) {return T> 0? T:-T ;}
Template <class T>
Class Stack {............};

4. Almost all syntax extensions can be replaced by one or more C ++ classes:
Set <int> S;
Int I;
Set_iter <int> ITER (s );
While (ITER. Next (I ))
...........
Compared with macros, we only sacrifice the simplicity of the program.

Of course, not all macros can be replaced (we do not advocate replacing all macros !), For
Macro, we should implement naming conventions for them, for example:
# Define company_xyz_libabc_macro stuff
At the same time, we must take some measures to prevent:
# Ifndef company_xyz_libabc_macro
# Define company_xyz_libabc_macro stuff
# Endif
Of course, macros defined internally in the library do not have this constraint:
My. cpp:
# Define macro stuff
........

We provide several common macros:
# Define a (x) T _ # x
# Define BX) # @ x
# Define CX) # x
Suppose: x = 1, there are:
A (1) ======= T_1
B (1) ====== '1'
C (1) = "1"
There is also a common macro: _ t
Tchar tstr [] = _ T ("T code ");
_ T macro is used to convert to tchar.

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.