为什么不reinter尺寸相同类型的pret_cast力copy_n的强制类型转换? [英] Why Doesn't reinterpret_cast Force copy_n for Casts between Same-Sized Types?
问题描述
根据 CP preference.com , reinter pret_cast
:
由reinter preting底层位模式类型之间的转换。
块引用>别急,这是一个谎言的原因,只在这些情况下工作:
当一个指针或引用类型
T1
的对象reinter pret_cast
(或C风格CAST)为指针或引用不同类型的T2
,演员总是成功的对象,但所产生的指针或引用只能访问如果两个T1
和T2
是标准的布局类型和下列条件之一:结果
T2
是(可能被cv修饰)动态类型对象李>
T2
和T1
都是(可能是多层次的,可能CV限定在每一级)的指针同一类型的T3
T2
是(可能CV-合格)签署或动态类型对象的无符号的变种
T2
是一个聚合类型或持有上述类型元素或非静态成员之一(包括递归的联合类型,subaggregates和非要素所包含的工会-static数据成员):这使得它安全地从一个结构的第一个成员,并从工会的元素的结构/联合包含它投
- 的
T2
是一个(可能是CV-限定)基类的动态类型的对象
T2
是字符
或unsigned char型
块引用>根据该列表中的非法的例子是:
自动富= 13LL;
自动栏= reinter pret_cast<双&安培;>(富);因此,为了使该投唯一可以接受的方法是复制内存:
自动富= 13LL;
双条;copy_n(reinter pret_cast<字符*>(安培;富),的sizeof(富),reinter pret_cast<字符*>(安培巴));我的问题是,为什么不
reinter pret_cast
处理一下吗?还是有别的东西可以让我不必通过这个箍跳?解决方案
为什么不
reinter pret_cast
处理一下吗?
块引用>的一个原因是尺寸,取向,和比特重presentations未指定,因此这种转换将不会是便携式的。但是,这会不会真的做辩解的行为定义,只是实现定义的。
通过使不确定,编译器允许假设不相关类型的前pressions不访问同一个对象,它可以让更好的优化。例如,在以下
INT和放大器;我的东西=();
浮球放; F = something_else();const int的I1 = I;
F = 42;
const int的I2 = I;编译器可以假定
I1
和I2
都具有相同的值(我
被分配到˚F
不变),并将其优化成一个常数。再破的假设会导致不确定的行为。
还是有别的东西可以让我不必通过这个箍跳?
块引用>复制字节是reinter preT一个对象类型作为一个不相关类型的唯一明确的方式。
与
reinter pret_cast走样
或联合有时会工作(假设大小等比赛),但如果优化器变得与太聪明也许你绊倒未定义的行为。According to cppreference.com,
reinterpret_cast
:Converts between types by reinterpreting the underlying bit pattern.
But wait, that's a lie cause it only works in these cases:
When a pointer or reference to object of type
T1
isreinterpret_cast
(or C-style cast) to a pointer or reference to object of a different typeT2
, the cast always succeeds, but the resulting pointer or reference may only be accessed if bothT1
andT2
are standard-layout types and one of the following is true:
T2
is the (possibly cv-qualified) dynamic type of the objectT2
andT1
are both (possibly multi-level, possibly cv-qualified at each level) pointers to the same typeT3
T2
is the (possibly cv-qualified) signed or unsigned variant of the dynamic type of the objectT2
is an aggregate type or a union type which holds one of the aforementioned types as an element or non-static member (including, recursively, elements of subaggregates and non-static data members of the contained unions): this makes it safe to cast from the first member of a struct and from an element of a union to the struct/union that contains it.T2
is a (possibly cv-qualified) base class of the dynamic type of the objectT2
ischar
orunsigned char
According to that list an illegal example would be:
auto foo = 13LL; auto bar = reinterpret_cast<double&>(foo);
So the only acceptable way to make that cast is to copy the memory:
auto foo = 13LL; double bar; copy_n(reinterpret_cast<char*>(&foo), sizeof(foo), reinterpret_cast<char*>(&bar));
My question is, why doesn't
reinterpret_cast
handle that for me? Or is there something else available so I don't have to jump through this hoop?解决方案why doesn't
reinterpret_cast
handle that for me?One reason is that the size, alignment, and bit representations aren't specified, so such a conversion wouldn't be portable. However, that wouldn't really justify making the behaviour undefined, just implementation-defined.
By making it undefined, the compiler is allowed to assume that expressions of unrelated types don't access the same object, which can allow better optimisation. For example, in the following:
int & i = something(); float & f = something_else(); const int i1 = i; f = 42; const int i2 = i;
the compiler can assume that
i1
andi2
both have the same value (i
being unchanged by the assignment tof
), and optimise them into a single constant. Breaking the assumption will then cause undefined behaviour.Or is there something else available so I don't have to jump through this hoop?
Copying the bytes is the only well-defined way to reinterpret one object type as an unrelated type.
Aliasing with
reinterpret_cast
or a union might work sometimes (assuming the size etc. match), but might trip you up if the optimiser gets too clever with undefined behaviour.这篇关于为什么不reinter尺寸相同类型的pret_cast力copy_n的强制类型转换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!