为什么我不能static_cast之间char *和unsigned char *? [英] Why can't I static_cast between char * and unsigned char *?

查看:2269
本文介绍了为什么我不能static_cast之间char *和unsigned char *?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

显然,编译器认为它们是不相关的类型,因此需要 reinterpret_cast 。为什么是这个规则?

Apparently the compiler considers them to be unrelated types and hence reinterpret_cast is required. Why is this the rule?

推荐答案

它们是完全不同的类型,见标准:

They are completely different types see standard:


3.9.1基本类型[basic.fundamental]

3.9.1 Fundamental types [basic.fundamental]

1声明为字符char的对象应足够大到
存储实现的基本字符集的任何成员。如果该集合中的
字符存储在字符对象中,则该字符对象的整数
值等于该字符的单个
字符文字形式的值。它是
实现定义一个char对象是否可以保存负
值。字符可以显式声明为unsigned或

签名。 一个char,一个签名的char和一个unsigned char
占据相同的存储量和具有相同的对齐方式
要求( basic.types );也就是说,它们具有相同的对象
表示。对于字符类型,对象的所有位

表示参与值表示。对于无符号
字符类型,值表示
的所有可能的位模式表示数字。这些要求不适用于其他类型。在
任何特定的实现中,一个简单的char对象可以接受
与signed char或unsigned char相同的值;其中一个是
实现定义的。

1 Objects declared as characters char) shall be large enough to store any member of the implementation's basic character set. If a character from this set is stored in a character object, the integral value of that character object is equal to the value of the single character literal form of that character. It is implementation-defined whether a char object can hold negative values. Characters can be explicitly declared unsigned or
signed. Plain char, signed char, and unsigned char are three distinct types. A char, a signed char, and an unsigned char occupy the same amount of storage and have the same alignment requirements (basic.types); that is, they have the same object representation. For character types, all bits of the object
representation participate in the value representation. For unsigned character types, all possible bit patterns of the value representation represent numbers. These requirements do not hold for other types. In any particular implementation, a plain char object can take on either the same values as a signed char or an unsigned char; which one is implementation-defined.

这么说也是为什么以下失败:

So analogous to this is also why the following fails:

unsigned int* a = new unsigned int(10);
int* b = static_cast<int*>(a); // error different types

code> b 是完全不同的类型,真正的问题是为什么static_cast当它可以执行下面没有问题时限制

a and b are completely different types, really what you are questioning is why is static_cast so restrictive when it can perform the following without problem

unsigned int a = new unsigned int(10);
int b = static_cast<int>(a); // OK but may result in loss of precision

为什么它不能推断目标类型相同的位域宽度可以表示吗?它可以为标量类型,但对于指针,除非目标是从源,并且您希望执行一个向下转换,然后在指针之间的转换将无法工作。

and why can it not deduce that the target types are the same bit-field width and can be represented? It can do this for scalar types but for pointers, unless the target is derived from the source and you wish to perform a downcast then casting between pointers is not going to work.

Bjarne Stroustrop说明为什么 static_cast 在此链接中很有用: http://www.stroustrup.com/bs_faq2.html#static-cast ,但是缩写形式是让用户清楚地说明他们的意图,并给编译器机会检查你的是因为 static_cast 不支持在不同的指针类型之间转换,所以编译器可以捕获这个错误来提醒用户,如果他们真的想做这个转换那么应该使用 reinterpret_cast

Bjarne Stroustrop states why static_cast's are useful in this link: http://www.stroustrup.com/bs_faq2.html#static-cast but in abbreviated form it is for the user to state clearly what their intentions are and to give the compiler the opportunity to check that what you are intending can be achieved, since static_cast does not support casting between different pointer types then the compiler can catch this error to alert the user and if they really want to do this conversion they then should use reinterpret_cast.

这篇关于为什么我不能static_cast之间char *和unsigned char *?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆