αλεχολυτ
αλεχολυτ

Reputation: 5060

Can static_cast gives nullpt for non-nullptr argument?

The following code gives me a warning "null pointer dereference" despite checking the pointer value before casting:

struct ID { virtual ~ID() = default; };
struct IF { virtual void g() = 0; };
struct F : IF { void g() {} };
struct D : ID, F { };

int main() {
    D* d;
    if (d) static_cast<IF*>(d)->g();
}

Is it based on using uninitialized pointer value with -O2 optimization mode or static_cast really can result in null pointer value for non-null argument (e.g. when multiple inheritance is in action)?

Upvotes: 0

Views: 244

Answers (0)

Related Questions