C++中的wchar_t(转)
以下討論都基于C++的定義
在C++的標(biāo)準(zhǔn)定義中
Type wchar_t is a distinct type whose values can represent distinct
codes for all members of the largest extended character set specified
among the supported locales. Type wchar_t shall have the same
size, signedness, and alignment requirements as one of the other
integral types, called its underlying type.
...
A character literal that begins with the letter L, such as L'x', is a
wide-character literal. A wide-character literal has type wchar_t.
The value of a wide-character literal containing a single c-char has
value equal to the numerical value of the encoding of the c-char in
the execution wide-character set. The value of a wide-character lit-
eral containing multiple c-chars is implementation-defined.
??? 按照我的理解,這意味著:
??? 1. 編譯器需要保證wchar_t與某種整數(shù)類型擁有相同的大小,符號(hào),對(duì)齊要求.這個(gè)"某種整數(shù)類型"是由編譯器定義的.這意味著 wchar_t(0x8000 > 1)的結(jié)果是取決于編譯器的. 在VC里面,如果設(shè)置了編譯選項(xiàng)"/Zc:wchar_t-",那么wchar_t會(huì)被默認(rèn)定義為無(wú)符號(hào)的"unsigned short".另外"signed wchar_t"或"unsigned wchar_t"在C++是不存在的
??? 2.與C不同,wchar_t必須是一個(gè)內(nèi)建(build-in)類型,我猜這是為了重載和模板特化的需要,看看iostream重載的類型大概就明白了.這里可以引申出另外一個(gè)話題: char 既不是 "signed char", 也不是"unsigned char"
??? 3.C++的標(biāo)準(zhǔn)文本沒(méi)有將wchar_t 和 unicode關(guān)聯(lián)起來(lái),標(biāo)準(zhǔn)僅僅要求,wchar_t 能夠用唯一的編碼表示該編譯器所有l(wèi)ocale的字符全集中的任何一個(gè)字符即可,雖然從需求上來(lái)講,wchar_t 也是一種unique-code,但是,一個(gè)特立獨(dú)行的編譯器,完全有權(quán)利定義一套完全不同于unicode的hexie-code,不過(guò),這個(gè)hexie-code至少需要保證在數(shù)值上,能夠兼容 char 類型的表數(shù)范圍,通常意味著 hexie-code的 0-255必須是和char的0-255表示相同的字符
??? 4.眾所周知的,windows的wchar_t是16位,linux的wchar_t是32位
??? 5.截至到VC8, VC的C運(yùn)行庫(kù)是不支持UTF-8的,也就是說(shuō)setlocale(LC_CTYPE, "zh_CN.UTF-8")是無(wú)效的,setlocale(LC_CTYPE, "zh_CN.65001")也不行.單步跟蹤后,發(fā)現(xiàn)在getqloc.c中,有以下代碼
??? // verify codepage validity
??? if (!iCodePage || iCodePage == CP_UTF7 || iCodePage == CP_UTF8 ||
??????? !IsValidCodePage((WORD)iCodePage))
??????? return FALSE;
這段代碼是VC8中新加的,VC7中沒(méi)有,表現(xiàn)出來(lái)的不同是, VC8在setlocale的時(shí)候就失敗了,而VC7直到真的使用mbstowcs這些函數(shù)的時(shí)候才會(huì)失敗
http://hi.baidu.com/bbcallen/blog/item/e2e37b1b5add59d3ac6e7549.html
轉(zhuǎn)載于:https://www.cnblogs.com/duzouzhe/archive/2010/03/29/1700038.html
總結(jié)
以上是生活随笔為你收集整理的C++中的wchar_t(转)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: face recongnition
- 下一篇: 使用 XMLBeans 进行编程