Cast to incomplete type in C and C++

This compiles in C and C++ without any problems:

void* p = (struct this_does_not_exist *) -1;

Remove struct, compile as C++ and get an error:

cast.cpp
cast.cpp(1) : error C2065: 'this_does_not_exist' : undeclared identifier
cast.cpp(1) : error C2059: syntax error : ')'

Adding a forward declaration:

class this_does_not_exist;
void* p = (this_does_not_exist *) -1;

And it again compiles cleanly.

All examples conform the Standard but to be honest the first one is really odd.

GCC gives a warning at least.


Original post | Disclaimer

Comments