I encountered a problem:
int main() {
const void* p = NULL;
const int** pp = static_cast<const int**>(p); // E0171 invalid type conversion (1)
int* const* ppp = static_cast<int* const*>(p); // No error (2)
}
Why is the conversion (1) here invalid, while the (2) is allowed?
A
static_castis not allowed to removeconstfrom the pointed-to type, onlyconst_castis allowed to do that.(1) In the conversion:
We are removing the const qualification from the pointed-to type. Without using
const_castto remove constness completely (which is probably not correct), we need to do:(2) The other conversion:
... is perfectly fine, because we are preserving the const-ness of what was pointed to.
Note: if you are having trouble with figuring out C/C++ type syntax, I recommend using cdecl+, an online tool which converts syntax to English prose.