Just ran into this:
#include <iostream>
using namespace std;
int main(int argc, char** argv)
{
float *a = new float[10];
void **b;
b = static_cast<void**>(&a);
delete(a);
return 0;
}
macbook:C nils$ g++ -Wall -g -o static_cast static_cast.cpp
static_cast.cpp: In function ‘int main(int, char**)’:
static_cast.cpp:9: error: invalid static_cast from type ‘float**’ to type ‘void**’
macbook:C nils$ clang++ -Wall -g -o static_cast static_cast.cpp
static_cast.cpp:9:9: error: static_cast from 'float **' to 'void **' is not
allowed
b = static_cast<void**>(&a);
^~~~~~~~~~~~~~~~~~~~~~~
1 error generated.
macbook:C nils$
Why is it not allowed? While b = (void**)(&a); works.
$5.2.9/2 –
Let us take the case of Code snippet 1 as below
float *f;void *p = f;Here initialization of ‘p’ is well-formed. This is in accordance with $4.2
An rvalue of type “pointer to cv T,” where T is an object type, can be converted to an rvalue of type “pointer to cv void.”Now let us take the code in OP
In our case, ‘E’ is
'float **'and ‘T’ is'void **'So, whether static_cast will work for the attempted conversion if ‘p’ can be initialized as shown below
float **f;void **p = f;The initialization of ‘p’ is ill-formed as it is not a valid condition listed under
$4.10Now, coming to why
b = (void**)(&a);works?This is the case where an explicit cast is used (
$5.4). In this case, this explicit cast is equivalent ofreinterpret_cast($5.4/5). In this particular case, this conversion is allowed ($5.2.1/7).Does this help?