So this static_cast
code is totally legal:
int n = 13;
void* pn = static_cast<void*>(&n);
void** ppn = &pn;
Yet this has to be made into a reinterpret_cast
to compile:
int n = 13;
int* foo = &n;
void** bar = static_cast<void**>(&foo);
If I don't change it I get the error:
error C2440:
static_cast
: cannot convert fromint **
tovoid **
note: Types pointed to are unrelated; conversion requiresreinterpret_cast
, C-style cast or function-style cast
So I take it the issue is "the types are unrelated". Yet I still don't understand, if it's OK when going from an int*
to void*
how can they be unrelated as a int**
and a void**
?
int
is in no way related to void
. The same goes for int**
and void**
and so they cannot be converted using static_cast
.
void*
however, is special. Any data pointer type (including int*
) can be static_cast
into void*
and back, despite no type being related to void
(even further, the conversion to void*
doesn't need a cast, as it is implicit). int*
doesn't have this property, nor does void**
and nor does any other pointer besides void*
.
The additional freedoms that have been granted to void*
come with additional restrictions. void*
cannot be indirected, nor can it be used with pointer arithmetic. These restrictions are only possible because there can never be an object of type void
. Or from opposite point of view, objects of void
cannot exist because of these restrictions.
void**
cannot be given those freedoms, because it cannot be given the same restrictions. It cannot be given those restrictions because void*
objects do exist and they need to exist. If we couldn't indirect or iterate void**
, then we couldn't use arrays of void*
for example.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With