L,_T(),_TEXT()的秘密

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()的用法

参考下面这篇博客,写的挺好的

http://blog.csdn.net/awey_001/article/details/6130795

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值