(Env: Linux / RTOS, Compiler: gcc v.7.3.1)
Is there a risk or cost to useless casts?
Here's an example:
Project.h:
typedef int32 PRJ_Status_t;
...
#define PRJ_SUCCESS((PRJ_Status_t)0)
Project.cpp:
#include "Project.h"
bool ProjectApp::init()
{
...
return(status == static_cast<int32>(PRJ_SUCCESS));
}
All casts except for
dynamic_castare performed at compilation time, so there is no cost to them during runtime. The risks are inherent to the casting itself - it's forcing by definition, overriding the automatic safety mechanisms of the language. In case of "useless" cast, the only risk is impact on readability and maintenance of the code.