jjzjj

c++ - 忽略注释 : offset of packed bit-field without using "-Wno-packed-bitfield-compat"

coder 2024-02-06 原文

当我尝试编译以下 union 时,弹出此警告: 10:5: note: offset of packed bit-field 'main()::pack_it_in::<anonymous struct>::two' has changed in GCC 4.4

#pragma GCC diagnostic ignore "-Wpacked-bitfield-compat"
union pack_it_in {
    struct
    {
        uint8_t zero : 3;
        uint8_t one : 2;
        uint8_t two : 6;
        uint8_t three : 4;
        uint8_t four : 1;
    } __attribute__((packed)) u8_2;
    uint16_t u16;
};
#pragma GCC diagnostic pop

#pragma 不会忽略该注释。有没有办法让 #pragma 工作而不必使用 -Wno-packed-bitfield-compat因为我希望仅针对我的八个 union 中的两个忽略此警告?

最佳答案

刚刚遇到了类似的问题。似乎 gcc 只是不喜欢跨越类型宽度的位域(如示例中的 two 那样)?

如果将所有类型更改为 uint16_t,gcc 接受:

union pack_it_in {
    struct
    {
        uint16_t zero  : 3;
        uint16_t one   : 2;
        uint16_t two   : 6;
        uint16_t three : 4;
        uint16_t four  : 1;
    } __attribute__((packed)) u8_2;
    uint16_t u16;
};

布局是您想要的,即使这些成员的类型可能不是。

关于c++ - 忽略注释 : offset of packed bit-field without using "-Wno-packed-bitfield-compat",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41253759/

有关c++ - 忽略注释 : offset of packed bit-field without using "-Wno-packed-bitfield-compat"的更多相关文章

随机推荐