dynamic_cast from a const class

Andrey Belevantsev abel@ispras.ru
Tue Jun 11 11:30:00 GMT 2013


Hello,

Does the below code look like valid C++, or is it a G++ bug?  5.2.7 has

<...>
The dynamic_cast operator shall not cast away constness (5.2.11).

The other compilers I could check quickly (clang, icc, msvc) all reject the 
code, GCCs 3.3, 4.6, 4.7 and 4.8 accept the code.

Andrey


class A {
};

class B : public A {
};

int main() {
   A* a;
   B* b = new B();
   a = dynamic_cast<const A*>(b);
   return 0;
}



More information about the Gcc-help mailing list