Should I use wchar_t when using UTF-8?

后端 未结 2 535
醉酒成梦
醉酒成梦 2020-12-02 00:50

UTF-8 can encode in 1, 2, and up to 4 bytes. A single char on my system is 1 byte. Should I use wchar_t as a precaution so that I will be able to f

相关标签:
2条回答
  • 2020-12-02 01:02

    No, you should not! The Unicode 4.0 standard (ISO 10646:2003) notes that:

    The width of wchar_t is compiler-specific and can be as small as 8 bits. Consequently, programs that need to be portable across any C or C++ compiler should not use wchar_t for storing Unicode text.

    Under most circumstances, the "character nature" of UTF-8 text will not be relevant to your program, so treating it as an array of char elements, just like any other string, will be sufficient. If you need to extract individual characters, though, those characters should be stored in a type that is at least 24 bits wide (e.g, uint32_t), in order to accomodate all Unicode code points.

    0 讨论(0)
  • 2020-12-02 01:28

    wchar_t is not of much use if you want to make your code portable.

    On wikipedia

    The width of wchar_t is compiler-specific and can be as small as 8 bits. Consequently, programs that need to be portable across any C or C++ compiler should not use wchar_t for storing Unicode text. The wchar_t type is intended for storing compiler-defined wide characters, which may be Unicode characters in some compilers"

    Further ,

    Both C and C++ introduced fixed-size character types char16_t and char32_t in the 2011 revisions of their respective standards to provide unambiguous representation of 16-bit and 32-bit Unicode transformation formats, leaving wchar_t implementation-defined.

    0 讨论(0)
提交回复
热议问题