c++ - Why can't static_cast a double void pointer? -



c++ - Why can't static_cast a double void pointer? -

consider next piece of code:

void **v_dptr(nullptr); int **i_dptr = static_cast<int**>(v_dptr);

the above illustration produces next compile error:

static_cast 'void **' 'int **' not allowed

live demo

i know proper way cast void pointer other pointer type utilize static_cast. however, can't static_cast double void pointer double pointer of other type.

q: why can't static_cast double void pointer? what's proper way cast double void pointer?

when have void* , cast int*, there may or may not mathematical/width adjustment create instance of proper type, static_cast<> prepared do. when have pointer void* , want pointer int*, static_cast<> has no write access pointed-to void* object; not free adjust create sure it's valid int* such static_cast<> can succeed , homecoming pointer can used access valid int*. while on architectures may not matter, if standard allowed code break when ported. (keep in mind it's unreasonable expect static_cast<> arrange additional memory int* somewhere initialises using static_cast<int*>(the_void_ptr) - not have unexpected overheads, it'd need in thread specific memory or allocated dynamically , released somehow, , manner of code ends comparing pointer values break.)

if want forcefulness matter , promise compiler width of both types same , no mathematical adjustment necessary etc. - can utilize reinterpret_cast<> , boss around, making clear you're accepting risk of undefined behaviour.

c++ pointers void-pointers

Comments

Popular posts from this blog

php - Edges appear in image after resizing -

ios8 - iOS custom keyboard - preserve state between appearances -

Delphi change the assembly code of a running process -