0

I just learned about bit fields and now I am wondering why they can only be applied to member variables, rather than global or function level variables.

for something like

int main(){
    unsigned u : 3;
}

I get

error: expected ';' at end of declaration
    unsigned u : 3

So, why do bit fields not work for variables other than members?

lo tolmencre
  • 3,804
  • 3
  • 30
  • 60
  • I guess it will work only with struct. – Sumeet Oct 05 '16 at 07:15
  • @fquinner That one's about C, this one's about C++, but the same rationale probably covers both languages. Tricky. –  Oct 05 '16 at 07:18
  • There's no separate rationale for C++. C++ inherited them from C, and the changes are merely practical (i.e. a bitfield member can be `private` in C++, but that's not a very intentional change from C. It follows from the rule that all members obey access specifications) – MSalters Oct 05 '16 at 13:46

0 Answers0