Why can't I reinterpret_cast uint to int?

后端 未结 7 1107
借酒劲吻你
借酒劲吻你 2020-12-29 23:40

Here\'s what I want to do:

const int64_t randomIntNumber = reinterpret_cast (randomUintNumber);

Where randomUintNumber is of type

7条回答
  •  感动是毒
    2020-12-30 00:20

    reinterpret_cast is used for reinterpreting the storage of the object as a different object. If you don't want to go through standard wording, you can find all that reinterpret_cast can do here. Generally you can remember you have to work with pointer types.

    So, if you really want to reinterpret the bits used for your uint64_t as int64_t, then do this:

    int64_t randomIntNumber = reinterpret_cast (randomUintNumber);
    

    However if you just want to convert the object, preserving its value if possible ... just do what the compiler suggests and use static_cast instead.

提交回复
热议问题