Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Portably safe to pass NULL/zero to dynamic_cast?

People also ask

Can dynamic_cast return null?

dynamic_cast and references Because C++ does not have a “null reference”, dynamic_cast can't return a null reference upon failure. Instead, if the dynamic_cast of a reference fails, an exception of type std::bad_cast is thrown.

Can dynamic_cast throw exception?

dynamic_cast will no longer throw an exception when type-id is an interior pointer to a value type, with the cast failing at runtime. The cast will now return the 0 pointer value instead of throwing.

Can a static cast be null?

The static_cast operator converts a null pointer value to the null pointer value of the destination type. Any expression can be explicitly converted to type void by the static_cast operator. The destination void type can optionally include the const , volatile , or __unaligned attribute.

What does dynamic_cast int *>( return if the cast is valid?

If it is, dynamic_cast<Type*> returns a pointer; otherwise it returns NULL.


§5.2.7/4:

If the value of v is a null pointer value in the pointer case, the result is the null pointer value of type R.

So you don't have to check for a null pointer yourself. Same goes for operator delete, deleting a null pointer has no effect.


Yes, you can use dynamic_cast on a null pointer.


Yes, check 5.2.7.4 in standard.