Defending macros

zhaozj2021-02-17  49

Defending Macrosby Steve Donovan, The Author Of C By Example: "Underc" Learning EditionMar 15, 2002

C macros in C code have been considered bad manners for years now. In this article, Steve Donovan explains how using a few carefully chosen macros can result in clearer, more readable code (provided they are used in a disciplined way). This article is Provided courtesy of que.

The C preprocessor comes from the C legacy that many supporters of the language want to go away The preprocessor step does exactly that;. It compensates for some of the deficiencies of C by working on the source text, replacing symbolic constants by numbers, etc. Because these deficiencies have mostly been addressed, the separate preprocessing step seems very old-fashioned and inelegant. But the inclusion of files, conditional compilation, and so on depend on the preprocessor so strongly that no proposal to retire it has been successful. I think most people are in agreement that inline functions and constants are much better than macros. The basic problem is that the preprocessor actually does text substitution on what the compiler is going to see. So macro-defined constants appear to the compiler as plain numbers. If I Define Pi In The Old-Fashioned Way, As Follows, Then Subsequently The Debugger Will Have No Knowledge of this Symbolic Constant, Because ITLITERLY DID NOT SEE IT: # Define Pi 3.1412 Simple-Minded Text Substitution Can Easily Cause Havoc and Is Not Saved by Putting In Parentheses: #define sqr1 (x) x * x

#define sqr2 (x) (x) * (x)

...

SQR1 (1 Y) => 1 y * 1 Y Bad!

SQR2 (1 Y) => (1 Y) * (1 Y) OK

SQR2 (SIN (X)) * (sin (x)) OK - EVAL. Twice

SQR2 (i ) => (i ) * (i ) Bad - evAl. Twice! This Vulnerability to Side Effects Is The Most Serious Problem Affecting All Macros. (Yes, i Know The Traditional Hack in this Case, But it has a serious weakness.) Inline functions do a much better job, and are just as fast. Again, once macros get any larger than SQR, then you are lost if you are trying to browse for a macro symbol or trace through a macro call. Debuggability and browsability are often unappreciated qualities when evaluating coding idioms;. in this case, they agree that macros stink Another serious problem with macros is related to their lack of browsability;. they are completely outside the C scoping system You never know when a macro is going . to clobber your program text in some strange way (Potential namespace pollution problems are nothing compared to this one!) So, a naming convention is essential; any macros must be in uppercase and have at least one argument, so they do not conflict WITH ANY SYMBOLIC Constants. The only type-checking you have with macros will be for the number of arguments, so it's wise to use this. Of course, macros also have an important role to play in conditional compilation, so such symbols must also be distinctly named (initial underscores are useful) I have rehashed all these old issues so you can appreciate the strict limits we must place on any useful macro The first one I'll introduce is a modified version of the FOR macro:.. #define FOR (i, n) For (int i = 0, _ct = (n); i <_ct; i

) This is free of the side-effect problem because a temporary variable is used to contain the loop count. (I am assuming that the compiler has the proper scoping for variables declared like this! Both i and _ct must be private to the loop. The Microsoft compiler will finally be compliant on this irritating item this year.) If n is a constant, then a good compiler will eliminate the local variable, so no penalty for correctness is necessary here. My argument is that using fOR consistently leads to fewer . errors and improved code readability One problem with typing the for-statement is that the loop variable is repeated three times, so mistakes happen My favorite is typing an i instead of aj in a nested loop:. for (int i = 0; i

... Note Here That Slight Difference Are Invisible In The Lexical Noise. If i See for (K, M) I know it deviates from this pattern (like for (k = 0; k <= m; k )).. So, exceptional cases are made more visible I find it entertaining that these statement macros are actually safer in standard C because you can DEFINE LOCAL LOOP VARIABLES. IN My Article Called "Overdoing Templates," I Point Out That C IS Not Good at Internal Iterator: Void HAS_EXPIRED (SHAPE * PS)

{RETURN PS-> modified_time ()

...

INT CNT =

Std :: count_if (lsl.begin (), lsl.end (), has_expired; Compare this withlist :: itemator SLI;

INT CNT = 0;

For (Sli = lsl.begin (); SLI! = lsl.end (); SLI)

if ((* sli) -> modified_time ()

typeof (& x) px = & x1; I'm presenting typeof as a part of C because Bjarne Stroustrup would like to see it included in the next revision of the standard, and it's a cool feature that needs every vote it can get With it. , I can write the forall statement macro, And Express Our Example More Simply: #define forall (IT, C) /

For (TypeOf ((c) .begin ()) IT = (c) .begin (); /

IT! = (c) .end (); IT)

...

INT CNT = 0;

FORALL (SLI, LS)

IF ((* SLI) -> modified_time ()

For_each (PS, LS)

if (ps-> modified_time ()

Struct _Foreach {

TypeName C :: Iterator M_IT, M_END;

T & M_VAR;

_Foreach (C & C, T & T): M_VAR (T)

{m_it = c.begin (); m_end = c.end ();

BOOL get () {

Bool res = m_it! = m_end;

IF (res) m_var = * m_it;

Return res;

}

Void next () { m_it;}

}

#define for_each (v, c) /

For (_Foreach _fe (C, V); /

_fe.get (); _fe.next ()) The ForEach constructor requires two things:.. a reference and a container-like object These are only evaluated once as arguments, so there are no side effects So FOR_EACH is valid in a number of contexts. Please note that it is better for containers of pointers or small objects because copying of each element takes place in turn. The typeof operator is essential here because template classes will not deduce their types from their constructor arguments. But you can actually implement fOR_EACH without typeof, using the fact that function templates can deduce their argument types However, you can not declare the concrete type, so it must be derived from an abstract base and then created dynamically The listing can be found here.int i;.. string S = "Hello";

// gives 104 101 108 108 111

For_each (i, s) cout << i << '';

List LSS;

...

FOR_EACH (s, lss) ... // may involve excessive copying! How efficient is FOR_EACH? Tests with iterating through a list show that the typeof version is only about 20% slower than the explicit loop because the reference iterator code can be easily inlined. The standard version is nearly three times slower because of the virtual method calls. Even so, in a real application, its use would most likely have no discernible effect on the total run time. A serious criticism of statement macros is that they allow people to invent their own private language that ends up being less readable and maintainable However, in a large project, programmers will fashion an appropriate idiom for the job in hand;.. hopefully, they leave documentation about their choices You certainly do not need the preprocessor to generate a private language. One or two new control constructs can be introduced on a per-case basis without affecting readability adversely. I'm not suggesting that programmers should be given carte blanche to make their C look like Basic or Algol 68, but a case can be made for using statement macros to improve code readability. This is particularly true for the more informal code that gets generated in interactive exploration and test frameworking. Macros still remain outside of the language, and for this reason, I do not expect much support on this modest position It is interesting to speculate about what extra features C would need to support these custom control structures Here is what a statement template might look like..: Template

__statement for (T T, S e) for (int T = 0; T __ statement ISEQ (C) c.begin (), c.end ()

....

Copy (ISEQ (LS), Array; if Lexical Substitution Were More Closely Integrated Into The Language, The Preprocessor Could Final Finally Be Retired After a Long and Curious Career.

转载请注明原文地址:https://www.9cbs.com/read-30092.html

New Post(0)