Search code examples
c++compiler-errorsmingwstatic-castqualifiers

C++ with MinGW : ignored type qualifier on cast result type : but which qualifier is ignored?


I have abstract class A and derived class B that publicly inherits from A.

I pass a const pointer to a const A to a function. In this function I static_cast this to a const pointer to a const B and get the error type qualifiers ignored on cast result type (because we use -Werror=ignored-qualifiers).

What can be so wrong here ? I have all const qualifiers required, seems to me.

void foo(const A* const pA)
{
    const B* const pB = static_cast<const B* const>(pA); // Error
   
    // ... use pB ...
}

Edit : using dynamic_cast instead solves it. Why though ?


Solution

  • The top-level qualifier. That is, the second one here: static_cast<const B* const>(pA).

    The cast returns a pointer prvalue, and (non-class non-array) prvalues can't have cv-qualifiers, so const is silently ignored.