Is wchar_t just a typedef of unsigned short?
Solution 1
In short: in C may be in C++ no.
Widely. C defines wchar_t as typedef but in Unix it is generally 4 bytes (so generally not short) and in Windows 2 so it may be short.
Under C++ it is unique built-in type like char
or int
, so you can legally overload void foo(short x)
and void foo(wchar_t x)
Solution 2
For anyone else who may come across this answer because function calls in your Visual Studio project won't link, despite both parties taking wchar_t
(or a comparable type, such as LPCTSTR
with UNICODE #defined), and when you DUMPBIN the library's exports the function takes const unsigned short *
, be aware that VS allows you to switch off wchar_t
as a built-in type. If someone changes this in a library, and you don't hit the same compiler switch in your project, it will not link.
This option can be changed under "Project Properties>C/C++/Language/Treat WChar_t as Builtin type", it can also be changed via the "/Zc" option.
Solution 3
For C, wchar_t
is a typedef
. Whether it is a synonym for unsigned int
, whether it is an unsigned type at all, or whether it is 4 bytes, is implementation-defined.
In C++, wchar_t
is a distinct built-in type. Here, too, its size and signedness is implementation-defined.
Solution 4
No, it doesn't. It translates to 'a wide character.' Making any assumptions about what that happens to be on a particular platform is incorrect, and defeats the entire purpose of having a wchar_t in the first place.
The point of using an abstraction is to separate the semantic meaning of the type from its underlying representation.
Solution 5
wchar_t isn't required by the standard to be unsigned. It can also be signed. And there must be another type of the same size; but the standard doesn't explicitly say that that other type must be short.
"the same size, signedness, and alignment requirements as one of the other integral types, called its underlying type" (C++98 §3.9.1).
In C compilers this is a typedef, usually defined in stddef.h
Marlon
Updated on June 03, 2022Comments
-
Marlon almost 2 years
for example, does:
wchar_t x;
translate to:
unsigned short x;
-
dalle about 14 years... but in order to be C++ standards compliant it would never be a
typedef
. -
Artyom about 14 yearsNote
sizeof(wchar_t)
== 2 or 4 and it dependent on platform. Generally only windowswchar_t
has 16 bits. -
Jonathan Leffler about 14 years@dalle (and Artyom): yes, but the question asks about a typedef so it must be about C. The tags actually omitted the language (I've added C now), but the inference is clear.
-
sbi about 14 years@Jonathan: No, the question asked whether
wchar_t
is atypedef
. ThatC
tag is yours, not Marlon's. The correct answer would be Yes for C, but No, for C++. -
Artyom about 14 years@Jonathan, I removed C tag, because it is not clear for the question that he asks about C, so I assume that the author should clarify if this is C only question or not.
-
AProgrammer about 14 yearsIIRC, AIX has also a 16 bits wchar_t and some embedded platforms have sizeof(wchar_t) == 1
-
Jonathan Leffler about 14 years@Artyom: OK...I think there are two subtly different but valid interpretations of the question, and we're tackling the two different versions. Version 1 is "Is wchar_t a typedef, and if so is it a typedef equivalent to unsigned short" - which every one else is assuming. Version 2 is "Given that wchar_t is a typedef, is it a typedef of unsigned short". Version 1 allows for confusion with C++; version 2 does not (because wchar_t is not a typedef in C++ so the question can't be about C++). It seemed 'obvious' to me that it was version 2; clearly, others see version 1 as more plausible.