分享
 
 
 

Defending Macros

王朝other·作者佚名  2006-01-08
窄屏简体版  字體: |||超大  

Defending Macros

by Steve Donovan, the Author of C++ by Example: "UnderC" Learning Edition

MAR 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 it literally 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))*(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 don't 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 a j in a nested loop:

for(int i = 0; i < n; i++)

for(int j = 0; i < m; j++)

...

Note here that slight differences are invisible in the lexical noise. If I see FOR(k,m), then I know this is a normal k = 0..m-1 loop, whereas if I see a for-statement 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 iterators. Here is a typical situation:

void has_expired(Shape *ps)

{ return ps->modified_time() < expiry_time; }

...

int cnt =

std::count_if(lsl.begin(),lsl.end(),has_expired);

Compare this with

list<Shape *>::iterator sli;

int cnt = 0;

for(sli = lsl.begin(); sli != lsl.end(); ++sli)

if ((*sli)->modified_time() < expiry_time) ++cnt;

This is much better behaved; the condition within the loop is explicit, and the scope of expiry_time (which is effectively global in the first version) can be local. The explicit declaration of an iterator does make this more verbose, however, and it's good practice to use typedefs (such as 'ShapeList') here.

I want to introduce a few candidate statement macros to make this common pattern even easier on the eye. First, let me introduce GCC's typeof operator, which makes a lot of template trickery quite straightforward. It takes an expression (which, as with sizeof, is not evaluated), and deduces its type, and can be used in declarations wherever a type is required:

double x1 = 2.3;

typeof(x) x2 = x1;

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() < expiry_time) ++cnt;

This statement macro works with any container-like object; that is, any type that defines an iterator and begin()/end(). But it suffers from the side-effect problem. It should not be called when the container argument is some non-trivial expression, such as a function call, because that expression must be evaluated for each iteration of the loop. And there is no way to enforce that restriction, because macros are too dumb. So FORALL does not meet our criterion as a "safe" statement macro. Besides, it simply cannot be expressed in the standard language.

A better candidate is FOR_EACH, which is a construct that is found in many languages. For instance, AWK has 'for(i in array)' for iterating over all keys in an associative array, and Visual Basic (and now C#) has FOR EACH. I will show that FOR_EACH is a much better-behaved statement macro, and it can in fact be implemented using the standard language, although not so efficiently. This is what I want to be able to say:

int cnt = 0;

Shape *ps;

FOR_EACH(ps,ls)

if (ps->modified_time() < expiry_time) ++cnt;

Note that this form makes it hard to accidentally modify the list, and as a bonus, is rather more debuggable. I bring this up because debugging code using the standard containers can be frustrating. If sli is an iterator, then *sli is the value—but the built-in expression evaluators in gdb and Visual Studio can't understand these smart pointers.

The FOR_EACH construct needs a special kind of iterator that binds a variable reference to each object in turn. When we ask this iterator for the next element, it assigns the next value to the variable reference. Eventually, it signals to the caller that there are no more elements in the collection, and the loop can terminate. The implementation using typeof follows:

// foreach.h

template <class C, class T>

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<typeof(c),typeof(v)> _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 cannot 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<string> 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 don't 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 don't 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 <class T, class S>

__statement FOR(T t, S e) for(int t = 0; t < e; t++)

It would still probably involve a lexical substitution, but done by the compiler, not the preprocessor. FOR is now a proper C++ symbol and can be properly scoped. Most importantly, potential side effects would automatically be eliminated because e will only be evaluated once. The macro FORALL can now safely be defined. Here is an example that cannot be done reliably using the preprocessor; an alternative implementation of Bjarne Stroustrup's idea of input sequences.

template <class C>

__statement iseq(C c) c.begin(), c.end()

....

copy(iseq(ls),array);

If lexical substitution were more closely integrated into the language, then the preprocessor could finally be retired after a long and curious career.

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有