写的太好了。。

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. VS2013 Winform程序打包部署 InstallShield2015LimitedEdition

    VS2013 Winform程序打包部署 VS2013默认是没有安装打包程序的,需要手动安装,我安装的是 InstallShield2015LimitedEdition. 1.点击解决方案,右键,选择 ...

  2. centos vsftpd

    1.配置文件 # 是否允许匿名登录FTP服务器,默认设置为YES允许# 用户可使用用户名ftp或anonymous进行ftp登录,口令为用户的E-mail地址.# 如不允许匿名访问则设置为NOanon ...

  3. vb.net

    vb.net 教程: https://www.yiibai.com/vb.net/vb.net_overview.html vb.net 教程 https://www.w3cschool.cn/vb_ ...

  4. .net第四章内容总结

    4.2)   mdi 多文档界面 创建子窗体的childForm.text书上的做法childformnumber不存在 于是我自定义一个childforNumber起始为1: 在窗体下面显示所有已经 ...

  5. 一个权重的物体拷贝权重给多个(oneWeightToMany)

    你是否在做项目的时候经常会遇见一个物体带权重需要拷贝给其他物体,这时候其他物体多的数不胜数 你怎么办呢? 举例:假如一头狮子 身体你已经做好了,但是模型师把半个身体都做满了垂落的实体模型毛发,你是否感 ...

  6. github/gitee使用办法2

    打开自己的仓库 git pull 把内容复制过去 git add . 添加所有 查看状态 git status 提交 git commit -m 'XXX' 最后push git push 如果内容本 ...

  7. 学习笔记DL008:概率论,随机变量,概率分布,边缘概率,条件概率,期望、方差、协方差

    概率和信息论. 概率论,表示不确定性声明数学框架.提供量化不确定性方法,提供导出新不确定性声明(statement)公理.人工智能领域,概率法则,AI系统推理,设计算法计算概率论导出表达式.概率和统计 ...

  8. MFC程序打包方法

    目录 1. 新建工程 2. 设置信息 3. 其他设置 4. 生成安装包 1. 新建工程 在同一个解决方案下,新建一个Setup工程,工程名为SetupVSR. (1)在"解决方案资源管理器& ...

  9. Movist for Mac(高清媒体播放器)v2.0.7中文特别版

    Movist for Mac中文破解版是目前Mac平台上最好用的视频播放器,功能强大简单好用.movist mac版拥有美观简洁的用户界面,提供多种功能,支持视频解码加速高品质的字幕,全屏幕浏览,是与 ...

  10. git的基本应用(一)

    Git常用的命令: mkdir  文件夹名称           创建文件夹 git  init                     将文件夹交个git管理 ls -ah              ...