2018年10月25日 星期四

[轉貼分享]將巨集展開後連結

https://stackoverflow.com/questions/6669551/converting-string-macros-constants-to-wide-characters-unicode


#define WIDEN(quote) WIDEN2(quote)
#define WIDEN2(quote) L##quote

#define VERSIONSTR "Test V1.2.3"
#define VERSIONSTRW WIDEN(VERSIONSTR)

**補充說明**

搞懂了,原來 C++ 的巨集參數採用量子疊加技術,看這例子

#define WIDEN(quote) string xx##quote = quote
#define VERSIONSTR "Test V1.2.3"
#define VERSIONSTRW WIDEN(VERSIONSTR)

WIDEN(quote) 的 quote 是兩種狀態的疊加 -- VERSIONSTR + "Test V1.2.3"

若 quote 是單獨使用會塌縮成 "Test V1.2.3"
若 quote 和 ## 或 # 一起使用會塌縮成 VERSIONSTR

現在可以解釋原題了

#define WIDEN(quote) WIDEN2(quote) // 會展開成 WIDEN2("Test V1.2.3"),已無疊加
#define WIDEN2(quote) L##quote // 會展開成 L"Test V1.2.3"

#define VERSIONSTR "Test V1.2.3"
#define VERSIONSTRW WIDEN(VERSIONSTR) // VERSIONSTR 和 "Test V1.2.3" 的疊加





沒有留言:

張貼留言