EE_DSnP 板


LINE

1F:推 dryman:如果全部功能都测试完毕,可以将一些for回圈写成macro 10/22 05:43
2F:→ dryman:只要名称取得好,不妨碍阅读程式码的话,macro也是很棒的 10/22 05:46
3F:→ dryman:ex: FOR_PTR_TO_END 10/22 05:46
4F:推 timrau:这种macro自己用还好,trace别人的程式遇到这种会抓狂 10/22 07:15
5F:→ timrau:debugger实在是对macros几乎无能为力.... 10/22 07:15
来看看C++的发明人Bjarne Stroustrup怎麽看macros 摘要 1. Macros的定义并不会被scope还有其他的type rules挡住 -- 这常会造成问题 2. Macros是会被preprocess的,这使得人看到的code和compiler看到的code不一样 3. 做为一个程式维护员,看到把资讯"藏得很隐密"的macro,只能当场傻眼 4. 不细心书写的macros常常会造成程式设计师对自己写的程式「语意」上的误解-- 虽然macro只是个代换而已 5. Bjarne Stroustrup自己一点都不想再去改进C++中macro的机制 他认为用C++里面其他的语法可以很恰当地取代macro的使用 ============================================================ So, what's wrong with using macros? Bjarne Stroustrup 出处: http://www.research.att.com/~bs/bs_faq2.html#macro Macros do not obey the C++ scope and type rules. This is often the cause of subtle and not-so-subtle problems. Consequently, C++ provides alternatives that fit better with the rest of C++, such as inline functions, templates, and namespaces. Consider: #include "someheader.h" struct S { int alpha; int beta; }; If someone (unwisely) has written a macro called "alpha" or a macro called "beta" this may not compile or (worse) compile into something unexpected. For example, "someheader.h" may contain: #define alpha 'a' #define beta b[2] Conventions such as having macros (and only macros) in ALLCAPS helps, but there is no language-level protection against macros. For example, the fact that the member names were in the scope of the struct didn't help: Macros operate on a program as a stream of characters before the compiler proper sees it. This, incidentally, is a major reason for C and C++ program development environments and tools have been unsophisticated: the human and the compiler see different things. Unfortunately, you cannot assume that other programmers consistently avoid what you consider "really stupid". For example, someone recently reported to me that they had encountered a macro containing a goto. I have seen that also and heard arguments that might - in a weak moment - appear to make sense. For example: #define prefix get_ready(); int ret__ #define Return(i) ret__=i; do_something(); goto exit #define suffix exit: cleanup(); return ret__ void f() { prefix; // ... Return(10); // ... Return(x++); //... suffix; } Imagine being presented with that as a maintenance programmer; "hiding" the macros in a header - as is not uncommon - makes this kind of "magic" harder to spot. One of the most common subtle problems is that a function-style macro doesn't obey the rules of function argument passing. For example: #define square(x) (x*x) void f(double d, int i) { square(d); // fine square(i++); // ouch: means (i++*i++) square(d+1); // ouch: means (d+1*d+1); that is, (d+d+1) // ... } The "d+1" problem is solved by adding parentheses in the "call" or in the macro definition: #define square(x) ((x)*(x)) /* better */ However, the problem with the (presumably unintended) double evaluation of i++ remains. And yes, I do know that there are things known as macros that doesn't suffer the problems of C/C++ preprocessor macros. However, I have no ambitions for improving C++ macros. Instead, I recommend the use of facilities from the C++ language proper, such as inline functions, templates, constructors (for initialization), destructors (for cleanup), exceptions (for exiting contexts), etc. --



※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 140.112.218.128 ※ 编辑: bnsblue 来自: 140.112.218.128 (10/22 10:04)







like.gif 您可能会有兴趣的文章
icon.png[问题/行为] 猫晚上进房间会不会有憋尿问题
icon.pngRe: [闲聊] 选了错误的女孩成为魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一张
icon.png[心得] EMS高领长版毛衣.墨小楼MC1002
icon.png[分享] 丹龙隔热纸GE55+33+22
icon.png[问题] 清洗洗衣机
icon.png[寻物] 窗台下的空间
icon.png[闲聊] 双极の女神1 木魔爵
icon.png[售车] 新竹 1997 march 1297cc 白色 四门
icon.png[讨论] 能从照片感受到摄影者心情吗
icon.png[狂贺] 贺贺贺贺 贺!岛村卯月!总选举NO.1
icon.png[难过] 羡慕白皮肤的女生
icon.png阅读文章
icon.png[黑特]
icon.png[问题] SBK S1安装於安全帽位置
icon.png[分享] 旧woo100绝版开箱!!
icon.pngRe: [无言] 关於小包卫生纸
icon.png[开箱] E5-2683V3 RX480Strix 快睿C1 简单测试
icon.png[心得] 苍の海贼龙 地狱 执行者16PT
icon.png[售车] 1999年Virage iO 1.8EXi
icon.png[心得] 挑战33 LV10 狮子座pt solo
icon.png[闲聊] 手把手教你不被桶之新手主购教学
icon.png[分享] Civic Type R 量产版官方照无预警流出
icon.png[售车] Golf 4 2.0 银色 自排
icon.png[出售] Graco提篮汽座(有底座)2000元诚可议
icon.png[问题] 请问补牙材质掉了还能再补吗?(台中半年内
icon.png[问题] 44th 单曲 生写竟然都给重复的啊啊!
icon.png[心得] 华南红卡/icash 核卡
icon.png[问题] 拔牙矫正这样正常吗
icon.png[赠送] 老莫高业 初业 102年版
icon.png[情报] 三大行动支付 本季掀战火
icon.png[宝宝] 博客来Amos水蜡笔5/1特价五折
icon.pngRe: [心得] 新鲜人一些面试分享
icon.png[心得] 苍の海贼龙 地狱 麒麟25PT
icon.pngRe: [闲聊] (君の名は。雷慎入) 君名二创漫画翻译
icon.pngRe: [闲聊] OGN中场影片:失踪人口局 (英文字幕)
icon.png[问题] 台湾大哥大4G讯号差
icon.png[出售] [全国]全新千寻侘草LED灯, 水草

请输入看板名称,例如:BuyTogether站内搜寻

TOP