views:

484

answers:

6

for example, does:

wchar_t x;

translate to:

unsigned short x;
A: 

Not necessarily; it could be a 4-byte quantity, or indeed any other size chosen by the implementation.

It depends on the compiler.

Jonathan Leffler
... but in order to be C++ standards compliant it would never be a `typedef`.
dalle
Not true in C++, in C may be
Artyom
@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.
Jonathan Leffler
@Jonathan: No, the question asked _whether_ `wchar_t` is a `typedef`. That `C` tag is yours, not Marlon's. The correct answer would be _Yes_ for C, but _No_, for C++.
sbi
@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.
Artyom
@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.
Jonathan Leffler
+5  A: 

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)

Artyom
A: 

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

John Knoeller
Note `sizeof(wchar_t)` == 2 or 4 and it dependent on platform. Generally only windows `wchar_t` has 16 bits.
Artyom
IIRC, AIX has also a 16 bits wchar_t and some embedded platforms have sizeof(wchar_t) == 1
AProgrammer
A: 

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.

kyoryu
+3  A: 

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.

sbi
A: 

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.

Tim Keating