写的太好了。。

When you convert for example int(12) to unsigned float (12.0f) your processor needs to invoke some calculations as both numbers has different bit representation. This is what static_cast stands for.

On the other hand, when you call reinterpret_cast the CPU does not invoke any calculations. It just treats a set of bits in the memory like if it had another type. So when you convert int* to float* with this keyword, the new value (after pointer dereferecing) has nothing to do with the old value in mathematical meaning.

Example: It is true that reinterpret_cast is not portable because of one reason - byte order (endianness). But this is often surprisingly the best reason to use it. Let's imagine the example: you have to read binary 32bit number from file, and you know it is big endian. Your code has to be generic and works properly on big endian (e.g. ARM) and little endian (e.g. x86) systems. So you have to check the byte order. It is well-known on compile time so you can write constexpr function:

constexpr bool is_little_endian() {
std::uint16_t x=0x0001;
auto p = reinterpret_cast<std::uint8_t*>(&x);
return *p != 0;
}

Explanation: the binary representation of x in memory could be 0000'0000'0000'0001 (big) or 0000'0001'0000'0000 (little endian). After reinterpret-casting the byte under p pointer could be respectively 0000'0000 or 0000'0001. If you use static-casting, it will always be 0000'0001, no matter what endianness is being used.

when to use reinterpret_cast的更多相关文章

  1. c++ 数据类型转换: static_cast dynamic_cast reinterpret_cast const_cast

    c++ 数据类型转换: static_cast dynamic_cast reinterpret_cast const_cast  [版权声明]转载请注明出处 http://www.cnblogs.c ...

  2. static_cast、dynamic_cast、reinterpret_cast、const_cast以及C强制类型转换的区别

    static_cast 1. 基础类型之间互转.如:float转成int.int转成unsigned int等 2. 指针与void*之间互转.如:float*转成void*.CBase*转成void ...

  3. C++标准转换运算符reinterpret_cast

    C++标准转换运算符reinterpret_cast reinterpret_cast <new_type> (expression) reinterpret_cast运算符是用来处理无关 ...

  4. c++强制类型转换:dynamic_cast、const_cast 、static_cast、reinterpret_cast

    c++强制类型转换:dynamic_cast.const_cast .static_cast.reinterpret_cast 博客分类: C/C++ CC++C#编程数据结构  dynamic_ca ...

  5. static_cast dynamic_cast const_cast reinterpret_cast总结对比

    [本文链接] http://www.cnblogs.com/hellogiser/p/static_cast-dynamic_cast-const_cast-reinterpret_cast.html ...

  6. C++-const_cast, reinterpret_cast, static_cast的用法

    /////////////////////////////////////////////////////////////////////////////// // // FileName : cas ...

  7. c++强制类型转换(static_cast,const_cast,dynamic_cast,reinterpret_cast)

    static_cast <typeid>(exdlvssion) static_cast 很像 C 语言中的旧式类型转换.它能进行基础类型之间的转换,也能将带有可被单参调用的构造函数或用户 ...

  8. static_cast和reinterpret_cast

    static_cast和reinterpret_cast 相同点:都是暴力转换,从一个类型转换为另一个类型,对于类指针不会保证安全性   static_cast和reinterpret_cast的区别 ...

  9. dynamic_cast,const_cast,static_cast,reinterpret_cast 详解

    如果直接指针直接强转,将只能访问虚函数的内容,而不能访问特定类中的特定成员或方法!!!! 强制类型转换运算符:C++有四种强制类型转换符,分别是dynamic_cast,const_cast,stat ...

  10. c++中的强制转换static_cast、dynamic_cast、reinterpret_cast的不同用法儿

    c++中的强制转换static_cast.dynamic_cast.reinterpret_cast的不同用法儿   虽然const_cast是用来去除变量的const限定,但是static_cast ...

随机推荐

  1. Vue源码之目录结构

    Vue版本:2.6.9 源码结构图 ├─ .circleci // 包含CircleCI持续集成/持续部署工具的配置文件 ├─ .github // 项目相关的说明文档,上面的说明文档就在此文件夹 ├ ...

  2. C语言-第4次作业得分

    作业链接:https://edu.cnblogs.com/campus/hljkj/CS201801/homework/2523 作业链接:https://edu.cnblogs.com/campus ...

  3. javascript的加减乘除结果会有误差,在两个浮点数相加的时候会比较明显。以下函数返回较为精确的计算结果

    加法函数(返回值:arg1加arg2的精确结果  ) function accAdd (arg1, arg2) { var r1, r2, m, c try { r1 = arg1.toString( ...

  4. 腾讯地图api 地址解析 js版

    <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" ...

  5. jdk的下载

    1.打开oracle的官网https://www.oracle.com/index.html 2.拖动页面到最后,找到java for developer 并点击 3. 4.拖动到最后找到java A ...

  6. centos安装mycat

    1.参考前文安装jdk 2.官网 http://www.mycat.io/ 或 http://dl.mycat.io/ 下载mycat 3.解压安装 cd /usr/local cp /home/ta ...

  7. uva 202

    #include <iostream> #include<cstdio> #include<cstring> #include<algorithm> # ...

  8. JavaScript对象(第四天)

    面向对象编程中,JavaScript并不完全具备封装.继承.多态:在JavaScript中,对象是一个无序的键值对集合 封装JavaScript是具备的,将属性和方法定义到对象内部: 继承,在java ...

  9. 必做作业3:原型化设计:地铁扫码app

    一.设计背景 伴随着地铁规模的快速扩张,使用手机扫码进出站成为了一种新型的地铁出行方式.在今天的北京和上海,地铁扫码已经极为普遍,广州和深圳也正在快速普及这种新方式.相信在不久的将来,其他拥有地铁或者 ...

  10. linux 域名解析

    vi /etc/hosts  中添加ip地址和域名  111.111.111.111   aa.swddjtc.cn   然后重启 /etc/init.d/network restart