【发布时间】:2018-04-27 11:44:09
【问题描述】:
是否可以编写一个自动迭代结构的所有成员的预处理器宏?
我有这样一个结构(从 Simulink 模型自动生成):
typedef struct {
real_T driveStatusword;
real_T posSensor[2];
real_T softAbortDemand;
} ExtU_motionCtrlRTOS_T;
还有一个类似的:
struct CoreInputOffsets
{
uint32_t driveStatusword;
uint32_t posSensor;
uint32_t softAbortDemand;
};
而我想做这样的操作:
void getCoreInputOffsets(CoreInputOffsets* pCoreInputOffsets)
{
pCoreInputOffsets->driveStatusword = offsetof(ExtU_motionCtrlRTOS_T, driveStatusword);
pCoreInputOffsets->posSensor = offsetof(ExtU_motionCtrlRTOS_T, posSensor);
pCoreInputOffsets->softAbortDemand = offsetof(ExtU_motionCtrlRTOS_T, softAbortDemand);
}
但不必在每次结构更改时编辑此函数,通过迭代CoreInputOffsets 的所有成员。
【问题讨论】:
-
C++ 还没有反射。
-
不能像为结构生成代码吗?
标签: c++ c-preprocessor