相关疑难解决方法(0)

类C结构中自动字段重新排序的方法

有没有一种方法可以在类似C的结构中执行自动字段重新排序?我的意思是使用语言功能(例如C和C ++的预处理器以及C ++的模板/类型特征/ etc),这使得可以执行以下宏(类似于Boost.Fusion的样式来适应结构):

REARRANGE(StructureName,
          (int8_t)(FieldName1),
          (int32_t)(FieldName2),
          (int16_t)(FieldName3),
          (int32_t)(FieldName4));
// is equivalent to (without loss of generality):
struct StructureName
{

    int32_t FieldName2;
    int32_t FieldName4;
    int16_t FieldName3;
    int8_t FieldName1;

};
Run Code Online (Sandbox Code Playgroud)

当然,方法应考虑字段的alignof值(以及sizeof),并在可能的情况下考虑#pragma pack当前值。

我知道结果的可移植性很差,但这仅用于本地使用。

必须将字段名称以及相应的类型保存在一起。

目的是减小总结构尺寸。

c c++ struct alignment c++14

5
推荐指数
1
解决办法
306
查看次数

标签 统计

alignment ×1

c ×1

c++ ×1

c++14 ×1

struct ×1