Look at this little snippet:
struct A {
virtual ~A() { }
};
struct B { };
bool fn() {
A *volatile a = new A;
return dynamic_cast<B *>(a);
}
Is the compiler allowed to remove the dynamic_cast altogether, and transform dynamic_cast to a simple nullptr;?
The reason of this question is this answer.
Notes:
Assume that volatile means that the compiler cannot assume anything about
a, because it's volatile. Here's a question why.The fact that
dynamic_castmay not be allowed to be removed is that there could be a type somewhere in the program, which derives from bothAandB.