C++ proper way to static_cast

c++, casting, exception, static-cast

Solution

static_cast does not give you information about success. If you need to do dynamic type casting use dynamic_cast or a library like boost any.

Problem

`static_cast`will not throw an exception. But if it does not succeed, it will produce a undefined result. What is the most proper way to check whether the cast succeeded? Will this help? ``` NewType new_typ_obj = static_cast<NewType>(obj); if (new_typ_obj) new_typ_obj.do(); ```

Original source