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 know what Linux does to support Unicode.)
The wchar_t type is an implementation-defined wide character type. In the Microsoft compiler, it represents a 16-bit wide character used to store Unicode encoded as UTF-16LE, the native character type on Windows operating systems.
This document sets out the walking, cycling and horse-riding assessment and review (WCHAR) process for highway schemes on motorways and all-purpose trunk roads.
Wide characters are similar to character datatype. The main difference is that char takes 1-byte space, but wide character takes 2-bytes (sometimes 4-byte depending on compiler) of space in memory. For 2-byte space wide character can hold 64K (65536) different characters. So the wide char can hold UNICODE characters.
See Wikipedia.
Basically, it's a portable type for "text" in the current locale (with umlauts). It predates Unicode and doesn't solve many problems, so today, it mostly exists for backward compatibility. Don't use it unless you have to.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With