static_cast - reinterpret_cast

Hi all;

What is the difference between casting void* to T* via static_cast or via reinterpret_cast? I mean are there cases when reinterpret_cast is more helpful when casting void* to T*.
when casting from void* it doesn't matter.

static_cast makes it so you can only do "safe" casts. That is, casts where the type you're casting to makes sense or can be converted.

For example casting from double* to int* would fail with static_cast because it isn't safe to interpret the raw data of a double as an integer. Therefore you would have to use reinterpret_cast.

Casting to/from void* is inherintly unsafe because you lose all type information. So I would recommend reinterpret_cast, but for whatever reason static_cast is allowed for those kinds of casts.
Topic archived. No new replies allowed.