#define CONST_FILENAME "okay.dat"
LPCWSTR lpFilename=L CONST_FILENAME; //obviously doesn't work
基本上,我如何获得相当于:
LPCWSTR lpFilename=L"okay.dat";
使用 #define
?
最佳答案
#define GLUE(x,y) x##y
#define W(x) GLUE(L,x)
#define CONST_FILENAME "okay.dat"
int main() {
const wchar_t* lpFilename = W(CONST_FILENAME);
const wchar_t* lpFilename = W("okay.dat");
wchar_t one_character = W('?');
}
编译证明在 http://ideone.com/2EzB6 .这正是 Microsoft 的 _T
宏的工作方式,除了我无条件地定义它之外,因此即使不在 MSVC“Unicode”构建中,您也可以使用它来获取宽字符串。至于为什么需要 GLUE 宏,我从来没有听过对我有意义的解释,但是没有它宏就不会扩展,所以它是必需的。看起来这里有详细信息:What are the applications of the ## preprocessor operator and gotchas to consider?
关于c++ - #define 带有 L 标识符的语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10051659/