Is wchar_t just a typedef of unsigned short?

荒凉一梦 提交于 2019-12-18 07:29:33

问题


for example, does:

wchar_t x;

translate to:

unsigned short x;

回答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)




回答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.




回答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.




回答4:


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




回答5:


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.




回答6:


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

It depends on the compiler.



来源:https://stackoverflow.com/questions/2395514/is-wchar-t-just-a-typedef-of-unsigned-short

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!