L,_T(),_TEXT()的秘密
jiese1990
L的含义
L"字符串" 表示字符串为unicode的字符串,每个字符占用两个字节。
_T()与_TEXT()源码
在tchar.h文件里有如下宏定义:
#ifdef __cplusplus //__cplusplus该宏代表是否是c++,如果不是c++是c语言的话就不会定义该宏
extern "C" {
#endif
...
#ifdef _UNICODE //_UNICODE代表工程是否使用Unicode字符,在工程属性--General-->Character Set里设置
#ifdef __cplusplus
} /* ... extern "C" */
#endif
#include <wchar.h>
#ifdef __cplusplus
extern "C" {
#endif
...
#define __T(x) L ## x //在tchar.h的第206行
//“##”:当c++宏展开时,##会作为连接操作符!如__T("jiese1990") --> L"jiese1990"
...
#else /* ndef _UNICODE */
#ifdef __cplusplus
} /* ... extern "C" */
#endif
#include <string.h>
#ifdef __cplusplus
extern "C" {
#endif
...
#define __T(x) x //在tchar.h的第850行
...
#ifdef __cplusplus
} /* ... extern "C" */
#endif
...
#endif /* _UNICODE */
#define _T(x) __T(x) //在tchar.h的第2388行
#define _TEXT(x) __T(x) //在tchar.h的第2389行
去掉c++的宏__cplusplus的部分再来看这段源码:
extern "C" {
...
//如果工程属性里设置了Use Unicode Character Set(使用Unicode字符集)
#ifdef _UNICODE
} /* ... extern "C" */
#include <wchar.h>
extern "C" {
..
#define __T(x) L ## x //在tchar.h的第206行
//“##”当c++宏展开时,##会作为连接操作符!即__T("jiese1990") --> L"jiese1990"
...
//如果没设置为Use Unicode Character Set设置成Use Multi-Byte Character Set了
#else /* ndef _UNICODE */
} /* ... extern "C" */
#include <string.h>
extern "C" {
...
#define __T(x) x //在tchar.h的第850行
...
} /* ... extern "C" */
...
#endif /* _UNICODE */
#define _T(x) __T(x) //在tchar.h的第2388行
#define _TEXT(x) __T(x) //在tchar.h的第2389行;
//_T()与_TEXT()两个宏是一样的
...
代码的意思在精简下差不多就是下面这样的意思(当然不能等价于下面这样):
#ifdef _UNICODE
#include <wchar.h>
#define __T(x) L ## x
#else
#include <string.h>
#define __T(x) x
#endif
#define _T(x) __T(x)
#define _TEXT(x) __T(x)
L,_T(),_TEXT()的用法
参考下面这篇博客,写的挺好的