Why can't static_cast a double void pointer?

后端 未结 5 1384
忘掉有多难
忘掉有多难 2021-01-12 06:05

Consider the following piece of code:

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

The above example produces

5条回答
  •  遥遥无期
    2021-01-12 06:51

    I believe this is an XY-problem. Depending on the circumstances it can be solved without reinterpret_cast. If you know that the void** pointer in fact points to a pointer to int, you can safely do this:

    int* iptr = static_cast(*v_dptr);
    

    Unless you really need int** in your code. And if you need, you can do:

    int** i_dptr = &iptr;
    

    but beware that it will point to a local variable iptr which will be destroyed when goes out of scope.

提交回复
热议问题