kei*_*ney 2 c embedded syntax compiler-optimization
在细读一些 STM32 中间件代码时,我遇到了这个非常奇怪的行并且无法解析它。基本上就是,
(void)(foo);
Run Code Online (Sandbox Code Playgroud)
这不是空指针转换 - 这很简单。它不是调用函数并将其返回值转换为 void - 这将需要更多的括号。它看起来像一个没有左值的右值。这只是防止功能被优化掉的空操作吗?或者它实际上有什么作用?
这是在上下文中。
// SVCCTL_EvtAckStatus_t is just an enum typedef
typedef SVCCTL_EvtAckStatus_t (*SVC_CTL_p_EvtHandler_t)(void *p_evt);
void SVCCTL_RegisterCltHandler( SVC_CTL_p_EvtHandler_t pfBLE_SVC_Client_Event_Handler )
{
#if (BLE_CFG_CLT_MAX_NBR_CB > 0)
// Ignore all this
SVCCTL_CltHandler.SVCCTL_CltHandlerTable[SVCCTL_CltHandler.NbreOfRegisteredHandler] = pfBLE_SVC_Client_Event_Handler;
SVCCTL_CltHandler.NbreOfRegisteredHandler++;
#else
// This is the weird stuff
(void)(pfBLE_SVC_Client_Event_Handler);
#endif
return;
}
Run Code Online (Sandbox Code Playgroud)
除了一件事,它什么都不做。阻止编译器抱怨未使用的变量。在 SO 上,实际上在这里推荐它作为一种可移植的方法:用于 C 和 C++ 的函数签名的可移植未使用参数宏
例子:
int somefunction(int a, int b, int c)
{
(void)(c); // c is reserved for future usage,
//stop the compiler from issuing "unused parameter" warning
return a+b;
}
Run Code Online (Sandbox Code Playgroud)