Are there C ++ tools that detect the misuse of static_cast, dynamic_cast and reinterpret_cast?

The answers to the following question describe the recommended use static_cast, dynamic_castand reinterpret_castin C ++:

When do you need to use static_cast, dynamic_cast, const_cast and reinterpret_cast?

Do you know any tools that can be used to detect abuse of these types of casts? Can a static analysis tool like PC-Lint or Coverity Static Analysis?

A particular case that raised this question was improper use static_castto lower the pointer, which the compiler does not warn about. I would like to detect this case with a tool and not assume that the developers will never make this mistake.

+3
source share
3 answers

Given that there is no reliable way to indicate which pointer points to compilation time, this is a rather difficult task that can be caught at compile time.

The easiest way is to make catch at run time using the safe_cast macro, which compiles into dynamic_cast using assert in debug and static_cast in version.

, , , NULL . , .

+5
+4

Boost polymorphic_pointer_downcast() polymorphic_downcast(), static_cast, ( , ), dynamic_cast.

. http://www.boost.org/doc/libs/1_61_0/libs/conversion/cast.htm.

0

Source: https://habr.com/ru/post/1737426/


All Articles