Why was wchar_t invented?

前端 未结 10 1656
说谎
说谎 2021-01-03 20:15

Why is wchar_t needed? How is it superior to short (or __int16 or whatever)?

(If it matters: I live in Windows world. I don\'t

相关标签:
10条回答
  • 2021-01-03 21:07

    Why is wchar_t needed? How is it superior to short (or __int16 or whatever)?

    In the C++ world, wchar_t is its own type (I think it's a typedef in C), so you can overload functions based on this. For example, this makes it possible to output wide characters and not to output their numerical value. In VC6, where wchar_t was just a typedef for unsigned short, this code

    wchar_t wch = L'A'
    std::wcout << wch;
    

    would output 65 because

    std::ostream<wchar_t>::operator<<(unsigned short)
    

    was invoked. In newer VC versions wchar_t is a distinct type, so

    std::ostream<wchar_t>::operator<<(wchar_t)
    

    is called, and that outputs A.

    0 讨论(0)
  • 2021-01-03 21:07

    Except for a small, ISO 2022 japanese minority, wchar_t is always going to be unicode. If you are really anxious you can make sure of that at compile time:

    #ifndef __STDC_ISO_10646__
    #error "non-unicode wchar_t, unsupported system"
    #endif
    

    Sometimes wchar_t is 16bits UCS-2 sometimes 32bits UCS-4, so what? Just use sizeof(wchar_t). wchar_t is NOT meant to be sent to disk nor to the network, it is only meant to be used in memory.

    See also Should UTF-16 be considered harmful? on this site.

    0 讨论(0)
  • 2021-01-03 21:11

    wchar_t is a bit of a hangover from before unicode standardisation. Unfortunately it's not very helpful because the encoding is platform specific (and on Solaris, locale-specific!), and the width is not specified. In addition there are no guarantees that utf-8/16/32 codecvt facets will be available, or indeed how you would access them. In general it's a bit of a nightmare for portable usage.

    Apparently c++0x will have support for unicode, but at the current rate of progress that may never happen...

    0 讨论(0)
  • 2021-01-03 21:15

    It is "superior" in a sense that it allows you to separate contexts: you use wchar_t in character contexts (like strings), and you use short in numerical contexts (numbers). Now the compiler can perform type checking to help you catch situations where you mistakenly mix one with another, like pass an abstract non-string array of shorts to a string processing function.

    As a side node (since this was a C question), in C++ wchar_t allows you to overload functions independently from short, i.e. again provide independent overloads that work with strings and numbers (for example).

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