引言
? ? ? ?學習redis源碼的過程中看到zmallo.h文件中有如下的宏定義:
? ? ? ?聯(lián)想到項目中也使用了這個技巧攒暇,當時使用這個技巧的原因在于形用,項目中定義了一個宏#define MARCS xxx,如果直接使用田度,還有一個宏為#define SPS_PRINT(a) #a。如果不在SPS_PRINT宏外側封裝另外一層宏乎莉,那么直接調用SPS_PRINT(MARCS)時奸笤,會發(fā)現(xiàn)沒有調用#xxx,而是直接將MARCS字符串化了边灭,這顯然不是我們預期的結果健盒。因此,需要在SPS_PRINT(a)外封裝第二個宏味榛,這個宏什么也不做予跌,直接調用SPS_PRINT(a),這樣频轿,第一層宏會將MARCS展開烁焙,將xxx傳遞給SPS_PRINT,接著SPS_PRINT做正確的字符串化操作膳殷。當時對這個只是一知半解九火,現(xiàn)在才知道他有個專業(yè)的名字--stringification of macro values,借著這個機會岔激,來探索下該技巧的內部機制。
正文
? ? ? ?先來看一段網(wǎng)上搜索到的解釋辱匿,
3.4 Stringification
Sometimes you may want to convert a macro argument into a string constant. Parameters are not replaced inside string constants, but you can use the ‘#’ preprocessing operator instead. When a macro parameter is used with a leading ‘#’, the preprocessor replaces it with the literal text of the actual argument, converted to a string constant. Unlike normal parameter replacement, the argument is not macro-expanded first. This is called stringification.
There is no way to combine an argument with surrounding text and stringify it all together. Instead, you can write a series of adjacent string constants and stringified arguments. The preprocessor will replace the stringified arguments with string constants. The C compiler will then combine all the adjacent string constants into one long string.
Here is an example of a macro definition that uses stringification:
#define WARN_IF(EXP) \
do { if (EXP) \
fprintf (stderr, "Warning: " #EXP "\n"); } \
while (0)
WARN_IF (x == 0);
==> do { if (x == 0)
fprintf (stderr, "Warning: " "x == 0" "\n"); } while (0);
The argument for EXP is substituted once, as-is, into the if statement, and once,stringified, into the argument to fprintf. If x were a macro, it would be expanded in the if statement, but not in the string.
The do and while (0) are a kludge to make it possible to write WARN_IF (arg);, which the resemblance of WARN_IF to a function would make C programmers want to do; see Swallowing the Semicolon.
Stringification in C involves more than putting double-quote characters around the fragment. The preprocessor backslash-escapes the quotes surrounding embedded string constants, and all backslashes within string and character constants, in order to get a valid C string constant with the proper contents. Thus, stringifying p = "foo\n";
results in "p = \"foo\\n\";". However, backslashes that are not inside string or character constants are not duplicated: ‘\n’ by itself stringifies to "\n".
All leading and trailing whitespace in text being stringified is ignored. Any sequence of whitespace in the middle of the text is converted to a single space in the stringified result. Comments are replaced by whitespace long before stringification happens, so they never appear in stringified text.
There is no way to convert a macro argument into a character constant.
If you want to stringify the result of expansion of a macro argument, you have to use two levels of macros.
#define xstr(s) str(s)
#define str(s) #s
#define foo 4
str (foo)
==> "foo"
xstr (foo)
==> xstr (4)
==> str (4)
==> "4"
s is stringified when it is used in str, so it is not macro-expanded first. But s is an ordinary argument to xstr, so it is completely macro-expanded before xstr itself is expanded (see Argument Prescan). Therefore, by the time str gets to its argument, it has already been macro-expanded.
? ? ? ?其實說白了絮短,就是預處理器在碰到#指令時昨忆,#的優(yōu)先級會高于宏展開的優(yōu)先級;所以需要額外定義一個外層宏,提前做好宏展開限府!