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燈, 水草

請輸入看板名稱,例如:Boy-Girl站內搜尋

TOP