1

This works:

struct A {
  unsigned int x = 0;
};

This also works:

struct A {
  unsigned int x : 1;
};

Is it possible to combine the two? I.e., initialize and specify size simultaneously? Or do I need to explicitly write a constructor to do this?

dshin
  • 2,354
  • 19
  • 29

1 Answers1

0

Let's take a quick look at the grammar of [class.mem]:

member-declarator:
    declarator virt-specifier-seqopt pure-specifieropt
    declarator brace-or-equal-initializeropt
    identifieropt attribute-specifier-seqopt : constant-expression

Bit fields are the third option, members with initializer are the second. So you cannot have both.

However, you can initialize named bit fields in constructor initializer lists, just like any other non-static data member. (But you cannot initialize unnamed bit fields, since they are not members.)

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084