其他分享
首页 > 其他分享> > reinterpret_cast and unIon cast 的区别

reinterpret_cast and unIon cast 的区别

作者:互联网

template < typename TO, typename FROM >
inline TO union_cast( FROM value )
{
    union { FROM from; TO to; } convert;
    convert.from = value;
    return convert.to;
}

正好在代码中看到一个union_cast的实现,还是第一次见到。
针对这个相当有趣的写法找到了一个相当有趣的回答:

taken together, this seems pretty clear - these constrain the implementation of pointer reinterpret_cast; the source and destination pointer shall point to the same object (have the same value), unless this is impossible due to alignment requirements. even if they have different values (due to alignment requirements), the union and reinterpret_cast would give identical results.

“源指针和目标指针应指向同一对象(同样的地址,所以具有相同的值),除非由于对齐要求而不可能。
即使它们具有不同的值(由于对齐要求),union和reinterpret_cast也会给出相同的结果。”

这句话的意思是,当int to double时,reinterpret_cast会失败,因为int的字节数量与double不匹配,编译器会抛出错误。
但是union所做的cast会成功,Union会按照最长的成员分配空间,不管int是几个字节,内存依然按照最长的成员double分配空间,转换不会失败。

Probably, on such computers C++ compiler (in theory) can generate a proper conversion code for reinterpret_cast (but not for unions, of course). A very good compiler is capable to generate a proper run-time check up of a good object alignment (in debug mode, for example) for reinterpret_cast - and so on.

“也许,在这种计算机上,C ++编译器(理论上)可以为reinterpret_cast生成适当的转换代码。一个非常好的编译器能够为reinterpret_cast等生成正确的运行时检查,以检查对象对齐是否正确(例如,在调试模式下)。”

reference: https://www.daniweb.com/programming/software-development/threads/141791/union-vs-reinterpret-cast#post676224

标签:convert,reinterpret,unIon,cast,编译器,alignment,union
来源: https://www.cnblogs.com/CookieBox/p/reinterpret_cast_and_unIon_cast.html