(环境:Linux / RTOS,编译器:gcc v.7.3.1)
无用的转换是否存在风险或成本?
这是一个例子:
项目.h:
typedef int32 PRJ_Status_t;
...
#define PRJ_SUCCESS((PRJ_Status_t)0)
项目.cpp:
#include "Project.h"
bool ProjectApp::init()
{
...
return(status == static_cast<int32>(PRJ_SUCCESS));
}
除了
dynamic_cast
之外的所有强制转换都在编译时执行,因此在运行时不会产生任何成本。风险是强制转换本身所固有的——根据定义,它是强制的,覆盖了语言的自动安全机制。