c++type-conversionstatic-cast

Why is the static_cast conversion invalid?


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?


Solution

  • A static_cast is not allowed to remove const from the pointed-to type, only const_cast is allowed to do that.

    (1) In the conversion:

    // pointer to CONST void     pointer to MUTABLE pointer to const int
       const void *          ->  const int **
    

    We are removing the const qualification from the pointed-to type. Without using const_cast to remove constness completely (which is probably not correct), we need to do:

    // pointer to CONST void     pointer to CONST pointer to const int
       const void *          ->  const int * const *
    
    // in code:
    auto pp = static_cast<const int * const *>(p);
    

    (2) The other conversion:

    // pointer to CONST void     pointer to CONST pointer to int
       const void *          ->  int * const *
    

    ... 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.