简体   繁体   English

为什么现在将wchar_t / unsigned short区分出来,但是却没有类似的char / unsigned字节区分?

[英]Why are wchar_t / unsigned short now distinct, but there is no analogous char / unsigned byte distinction?

It just seems like "not of one mind" in the design here, because integer data and character data of 16 bits is now differentiable but integer and character data of 8 bits is not. 在这里的设计中看起来好像“没主意”,因为现在可以区分16位的整数数据和字符数据,而没有8位的整数和字符数据。

C++ has always had the only choice for 8-bit values a 'char'. C ++一直是8位值“ char”的唯一选择。 But the feature of recognizing wchar_t as an official, distinct type from unsigned short enables improvements, but only for wide-string users. 但是,将wchar_t识别为正式的,与unsigned short截然不同的类型的功能可以改进,但仅适用于宽字符串用户。 It seems like this is not coordinated; 看来这是不协调的。 the language acts differently for 8-bit and 16-bit values. 对于8位和16位值,该语言的行为有所不同。

I think there is clear value in having more distinct types; 我认为拥有更多不同的类型具有明显的价值。 having distinct 8-bit char AND and 8-bit "byte" would be much nicer, eg in usage for operator overloading. 具有不同的8位字符AND和8位“字节”会更好,例如,用于运算符重载。 For example: 例如:

// This kind of sucks...
BYTE m = 59;     // This is really 'unsigned char' because there is no other option
cout << m;       // outputs character data ";" because it assumes 8-bits is char data.
                 // This is a consequence of limited ability to overload

// But for wide strings, the behavior is different and better...
unsigned short s = 59;
wcout << s;      // Prints the number "59" like we expect
wchar_t w = L'C'
wcout << w;      // Prints out "C" like we expect

The language would be more consistent if there were a new 8-bit integer type introduced, which would enable more intelligent overloads and overloads that behave more similarly irrespective of if you are using narrow or wide strings. 如果引入了新的8位整数类型,则该语言将更加一致,这将启用更智能的重载,并且无论您使用的是窄字符串还是宽字符串,重载的行为都更相似。

Yes, probably, but using single-byte integers that aren't characters is pretty rare and you can trivially get around your stated problem via integral promotion (try applying a unary + and see what happens). 是的,也许可以,但是使用字符的单字节整数非常少见,您可以通过整型提升来轻松解决所陈述的问题(尝试应用一元+并查看会发生什么)。

It's also worth noting that your premise is flawed: wchar_t and unsigned short have always been distinct types, per paragraph 3.9.1/5 in C++98, C++03, C++11 and C++14. 还值得注意的是,您的前提存在缺陷:根据C ++ 98,C ++ 03,C ++ 11和C ++ 14中的3.9.1/5段, wchar_tunsigned short 一直是不同的类型。

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

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