Reinterpret_cast与C风格的转换 [英] Reinterpret_cast vs. C-style cast

查看:216
本文介绍了Reinterpret_cast与C风格的转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我听说 reinterpret_cast 是实现定义的,但我不知道这是什么意思。

解决方案

你可以提供一个例子来说明如何出错, p> C风格的转换不是更好。



它只是尝试各种C ++风格的cast顺序,直到找到一个工作。这意味着当它像一个 reinterpret_cast ,它具有与 reinterpret_cast 完全相同的问题。但是,除此之外,它还有这些问题:




  • 它可以做很多不同的事情,并不总是清楚从读取代码哪种类型(它可能像 reinterpret_cast const_cast static_cast ,而那些做非常不同的事情),

  • 同样,更改周围的代码可能会改变演员的意思,

  • 很难搜索( reinterpret_cast 很容易找到,这是很好的,因为casts是丑陋的,应该注意使用时,但一个C风格的演员, (int)42.0 ,很难通过搜索找到)



要回答你的问题的另一部分,是的, reinterpret_cast 是实现定义的。这意味着当你使用它从 int * 转换为 float * 时,不能保证结果指针指向同一个地址。这部分是实现定义的。但是如果你把生成的 float * reinterpret_cast 代入 int * code>,那么你会得到原始的指针。



但是,请记住,无论您是使用 reinterpret_cast 还是C风格cast:

  int i; 
int * p0 =& i;

float * p1 =(float *)p0; //实现定义的结果
float * p2 = reinterpret_cast< float *>(p0); //实现定义的结果

int * p3 =(int *)p1; //保证p3 == p0
int * p4 =(int *)p2; //保证p4 == p0
int * p5 = reinterpret_cast< int *>(p1); //保证p5 == p0
int * p6 = reinterpret_cast< int *>(p2); //保证p6 == p0


I hear that reinterpret_cast is implementation defined, but I don't know what this really means. Can you provide an example of how it can go wrong, and it goes wrong, is it better to use C-Style cast?

解决方案

The C-style cast isn't better.

It simply tries the various C++-style casts in order, until it finds one that works. that means that when it acts like a reinterpret_cast, it has the exact same problems as a reinterpret_cast. But in addition, it has these problems:

  • it can do many different things, and it's not always clear from reading the code which type of cast will be invoked (it might behave like a reinterpret_cast, a const_cast or a static_cast, and those do very different things),
  • similarly, changing the surrounding code might change the meaning of the cast,
  • it's hard to search for (reinterpret_cast is easy to find, which is good, because casts are ugly and should be paid attention to when used. But a C-style cast, as in (int)42.0, is much harder to find by searching)

To answer the other part of your question, yes, reinterpret_cast is implementation-defined. This means that when you use it to convert from, say, an int* to a float*, then you have no guarantee that the resulting pointer will point to the same address. That part is implementation-defined. But if you take the resulting float* and reinterpret_cast it back into an int*, then you will get the original pointer. That part is guaranteed.

But again, remember that this is true whether you use reinterpret_cast or a C-style cast:

int i;
int* p0 = &i;

float* p1 = (float*)p0; // implementation-defined result
float* p2 = reinterpret_cast<float*>(p0); // implementation-defined result

int* p3 = (int*)p1; // guaranteed that p3 == p0
int* p4 = (int*)p2; // guaranteed that p4 == p0
int* p5 = reinterpret_cast<int*>(p1); // guaranteed that p5 == p0
int* p6 = reinterpret_cast<int*>(p2); // guaranteed that p6 == p0

这篇关于Reinterpret_cast与C风格的转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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