简体   繁体   English

wchar_t只是unsigned short的typedef吗?

[英]Is wchar_t just a typedef of unsigned short?

for example, does: 例如,做:

wchar_t x;

translate to: 翻译成:

unsigned short x;

In short: in C may be in C++ no. 简而言之:在C中可能是C ++没有。

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. C将wchar_t定义为typedef,但在Unix中它通常是4个字节(通常不短),而在Windows 2中它可能很短。

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) 在C ++下它是唯一的内置类型,如charint ,因此你可以合法地重载void foo(short x)void foo(wchar_t x)

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. 对于可能遇到此答案的任何其他人,因为Visual Studio项目中的函数调用不会链接,尽管双方都采用wchar_t (或类似的类型,例如LPCTSTR与UNICODE #defined),并且当您DUMPBIN库的导出时函数采用const unsigned short * ,请注意VS允许您将wchar_t作为内置类型关闭。 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. 此选项可在“项目属性> C / C ++ /语言/处理WChar_t作为内置类型”下更改,也可以通过“/ Zc”选项进行更改。

For C, wchar_t is a typedef . 对于C, wchar_t是一个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. 它是否是unsigned int的同义词,它是否是一个无符号类型,或者它是否是4个字节,是实现定义的。

In C++, wchar_t is a distinct built-in type. 在C ++中, wchar_t是一种独特的内置类型。 Here, too, its size and signedness is implementation-defined. 在这里,它的大小和签名也是实现定义的。

wchar_t isn't required by the standard to be unsigned. 标准不要求wchar_t是无符号的。 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). “与其他整数类型之一相同的大小,符号和对齐要求,称为其基础类型”(C ++98§3.9.1)。

In C compilers this is a typedef, usually defined in stddef.h 在C编译器中,这是一个typedef,通常在stddef.h中定义

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. 对特定平台上发生的事情做出任何假设是不正确的,并且首先违背了拥有wchar_t的整个目的

The point of using an abstraction is to separate the semantic meaning of the type from its underlying representation. 使用抽象的关键是将类型的语义含义与其底层表示分开。

Not necessarily; 不必要; it could be a 4-byte quantity, or indeed any other size chosen by the implementation. 它可以是4字节的数量,或实际上由实现选择的任何其他大小。

It depends on the compiler. 这取决于编译器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM