C++ When should we prefer to use a two chained static_cast over reinterpret_cast

前端 未结 6 1529
不思量自难忘°
不思量自难忘° 2020-12-04 01:29

First of all, this is not a duplicate of Why do we have reinterpret_cast in C++ when two chained static_cast can do it\'s job?.

I know situations where we cannot use

6条回答
  •  春和景丽
    2020-12-04 02:13

    Because in theory, they could do something different (although it's hard to imagine such a case). More importantly, they send different signals to the reader, and tell a different story to the compiler (which could affect optimization). Logically, I'd say use chained static_cast through void* for cases to/from a character type (e.g. dumping an image of the raw memory), and other cases which are well defined and portable, and reinterpret_cast when you're doing real low level, hardware dependent work: extracting the exponent field of a float by casting its address to an unsigned int*, and bit masking, for example.

提交回复
热议问题