Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't static_cast a double void pointer?

Consider the following piece of code:

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

The above example produces the following compile error:

static_cast from 'void **' to 'int **' is not allowed

LIVE DEMO

I know that the proper way to cast a void pointer to any other pointer type is to use static_cast. However, you can't static_cast a double void pointer to another double pointer of other type.

Q:

  1. Why we can't static_cast a double void pointer?
  2. What's the proper way to cast a double void pointer?
like image 713
101010 Avatar asked Oct 09 '14 11:10

101010


People also ask

Can we get Typeid of void pointer?

You can use the strcmp function to help you compare const char* strings. Then after that you called GetType function, now you know to which type to convert or cast the void pointer of the Object and then do whatever you want with the current iterated object!

Can void pointer be dereferenced?

Some Interesting Facts: 1) void pointers cannot be dereferenced. For example the following program doesn't compile.

What is static_cast void?

static_cast<void>() is the 'C++ way' of writing void conversion. In the en.cppreference.com website mentioned as discards the value of the expression. In below link four points on Explanation section. http://en.cppreference.com/w/cpp/language/static_cast. Where and Why should we use static_cast<void>() ?

What is the point of static_cast?

The static_cast operator converts variable j to type float . This allows the compiler to generate a division with an answer of type float . All static_cast operators resolve at compile time and do not remove any const or volatile modifiers.


2 Answers

When you have a void* and cast it to an int*, there may or may not be some mathematical/width adjustment to create an instance of the proper type, which static_cast<> will be prepared to do. When you have only a pointer to a void* and want a pointer to an int*, the static_cast<> has no write access to the pointed-to void* object; it is not free to adjust it to make sure it's a valid int* such that the static_cast<> can succeed and return a pointer which really can be used to access a valid int*. While on some architectures this may not matter, if the standard allowed this then the code could break when ported. (Keep in mind that it's unreasonable to expect the static_cast<> to arrange for some additional memory for an int* somewhere that it initialises using a static_cast<int*>(the_void_ptr) - not only would that have unexpected overheads, but it'd need to be in thread specific memory or allocated dynamically and released somehow, and all manner of code that actually ends up comparing the pointer values would break.)

If you want to force the matter and promise the compiler that the width of both types is the same and no mathematical adjustment is necessary etc. - then you can use reinterpret_cast<> and boss it around, making it clear you're accepting the risk of undefined behaviour.

like image 147
Tony Delroy Avatar answered Sep 22 '22 05:09

Tony Delroy


void* is special in that it can point to anything. It is a "pointer to unspecified type." Therefore, converting to and from void* to T* for some type T is a "normal" operation, one which static_cast can support. In effect, such a conversion says: "The pointer doesn't know to what it points, but I know it: it points to a T."

void** is not special special in this way. It can point to one thing only, to a void*. Converting a void** to an int** says something different: "The pointer claims it points to a void*, but I want to treat it as a pointer to int* instead." And if you want to treat one thing as a different thing, you want to reinterpret the original - so use reinterpret_cast.

like image 44
Angew is no longer proud of SO Avatar answered Sep 24 '22 05:09

Angew is no longer proud of SO