Bit-fields “In-class initialization” results in “error: lvalue required as left operand of assignment”

后端 未结 4 996
难免孤独
难免孤独 2020-12-06 16:29
struct bitfield {
  int i = 0;  // ok
  int j : 8 = 0;  // error: lvalue required as left operand of assignment
};

What is the correct syntax to in

4条回答
  •  Happy的楠姐
    2020-12-06 17:12

    You cannot (in C++11) in-class initialize bitfields.

    In MSVC and gcc (with extensions), the anonymous union and struct code lets you get around this a bit.

    struct bitfield {
      int i = 0;  // ok
      union {
        uint32_t raw = 0;
        struct {
          int j : 8;
          int x : 3;
        };
      };
    };
    

    where we mix a fixed size raw with a union over bitfields, then in-class initialize the raw element.

提交回复
热议问题