Is wchar_t just a typedef of unsigned short?

15,198

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

Share:
15,198
Marlon
Author by

Marlon

Updated on June 03, 2022

Comments

  • Marlon
    Marlon almost 2 years

    for example, does:

    wchar_t x;
    

    translate to:

    unsigned short x;
    
  • dalle
    dalle about 14 years
    ... but in order to be C++ standards compliant it would never be a typedef.
  • Artyom
    Artyom about 14 years
    Note sizeof(wchar_t) == 2 or 4 and it dependent on platform. Generally only windows wchar_t has 16 bits.
  • Jonathan Leffler
    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
    sbi about 14 years
    @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++.
  • Artyom
    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
    AProgrammer about 14 years
    IIRC, AIX has also a 16 bits wchar_t and some embedded platforms have sizeof(wchar_t) == 1
  • Jonathan Leffler
    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.