看板 C_and_CPP 關於我們 聯絡資訊
※ 引述《loveflames (咕啾咕啾魔法陣)》之銘言: : #define FOO_VALUE 5 : #define N5 ~,1 : #define N11 ~,1 : #define N18 ~,1 : #define N21 ~,1 : #define N29 ~,1 : #define Result1 1//5,11,18,21,29 : #define Result0 //其他,0對應到TEST的n : #define TEST(x,y,n) _TEST(x,y,n)//在串接前先展開x與y : #define _TEST(x,y,n) __TEST(x##y,n) : #define __TEST(x,n) ___TEST(x,n)//須多展開一次,不定參數先確認參數數量才展開參數 : #define ___TEST(d,n,...) n : #define CAT(a,b) _CAT(a,b) : #define _CAT(a,b) a##b : : //上面的code可以獨立出來包在header內 : : #if CAT(Result,TEST(N,FOO_VALUE,0)) > 0 : //不做任何事,Result0會導致編譯錯誤 : #endif : → Hazukashiine: 底線後面接大寫字母是保留給編譯器實作的 盡量避免 07/11 18:25 : → Hazukashiine: #define Result0 0 // ... 07/12 01:57 : 推 LPH66: 他就是要 Result0 編譯錯誤...不過倒是可以 #else #error 07/12 04:31 : → loveflames: if/else的寫法,如果合法值要改就得改header 07/12 09:21 原本我是想說直接 #if TEST(...) > 0 #else #error #endif 這樣的, 不過轉念一想, 過程中確實能產出一個數字, 那直接就判斷數字就好了 還能省去 > 0 跟一個連接; 於是就變成以下這樣 #define FOO_VALUE 5 /* 要檢查的值 */ #define N5 ~,0 /* 這邊都一樣, 不過我把 0 和 1 對調 */ #define N11 ~,0 #define N18 ~,0 #define N21 ~,0 #define N29 ~,0 #define TESTNOT(v) _TESTNOT(v) /* 因為 0 1 對調所以改叫 TESTNOT */ #define _TESTNOT(v) SECOND(N##v,1) /* 這裡把後半段改叫 SECOND, 然後參數改 1 */ #define SECOND(x,n) _SECOND(x,n) #define _SECOND(d,n,...) n #if TESTNOT(FOO_VALUE) #error Invalid FOO_VALUE! #endif 而且以情境來看, #error 還能告訴使用者是哪裡有問題 -- 'Oh, Harry, don't you see?' Hermione breathed. 'If she could have done one thing to make absolutely sure that every single person in this school will read your interview, it was banning it!' ---'Harry Potter and the order of the phoenix', P513 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 180.177.29.238 ※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1499872037.A.14D.html
loveflames: 推簡化 07/13 09:30