I am new to C++ style casts and I am worried that using C++ style casts will ruin the performance of my application because I have a real-time-critical deadline in my interrupt-service-routine.
I heard that some casts will even throw exceptions!
I would like to use the C++ style casts because it would make my code more ‘robust’. However, if there is any performance hit then I will probably not use C++ style casts and will instead spend more time testing the code that uses C-style casts.
Has anyone done any rigorous testing/profiling to compare the performance of C++ style casts to C style casts?
What were your results?
What conclusions did you draw?
If the C++ style cast can be conceptualy replaced by a C-style cast there will be no overhead. If it can’t, as in the case of
dynamic_cast, for which there is no C equivalent, you have to pay the cost one way or another.As an example, the following code:
generates identical code for both casts with VC++ – code is:
The only C++ cast that can throw is
dynamic_castwhen casting to a reference. To avoid this, cast to a pointer, which will return 0 if the cast fails.