6

Can anyone explain the reason behind not allowing bit fields as static member of a class? For example, a class defined like:

class A{
public:
    A() {}
    ~A(){}
private:
    static int mem :10;
};
int A::mem;

doesn't compile.

Compiling this class with different compilers:-

1- g++ throws error:-

error: static member 'mem' cannot be a bit-field

static int mem :10;

error: ‘int A::mem’ is not a static data member of ‘class A’

int A::mem;

2- clang throws error:-

error: static member 'mem' cannot be a bit-field

static int mem :10;

3-Visual Studio 15 throws error:-

'A::mem'::illegal storage class

'int A::mem':member function redeclaration not allowed

Jesper Juhl
  • 30,449
  • 3
  • 47
  • 70
Dinesh Maurya
  • 822
  • 8
  • 24

2 Answers2

7

The main reason is because that's what the C++ standard says explicitly:

[class.bit] 12.2.4/3

A bit-field shall not be a static member. A bit-field shall have integral or enumeration type ([basic.fundamental]). A bool value can successfully be stored in a bit-field of any nonzero size. The address-of operator & shall not be applied to a bit-field, so there are no pointers to bit-fields. A non-const reference shall not be bound to a bit-field ([dcl.init.ref]). [ Note: If the initializer for a reference of type const T& is an lvalue that refers to a bit-field, the reference is bound to a temporary initialized to hold the value of the bit-field; the reference is not bound to the bit-field directly. See [dcl.init.ref].  — end note ]

The reasoning for it? Well, bit-fields are a carry-over from C. They are allowed only as struct or union fields there to begin with. Personally, I can't think of a context where a static bit-field member can be useful.

Furthermore, practically everything about bit-fields is implementation defined already, and letting static data behave in a completely implementation defined manner, is IMHO a very bad idea.

StoryTeller - Unslander Monica
  • 165,132
  • 21
  • 377
  • 458
  • @StorTeller, as you said "practically everything about bit-fields is implementation defined", but then standard explicitly mentions that "A bit-field shall not be a static member". So the standard committee must have some reason to disallow static bit field members. May be something on the lines mentioned by Jeremy. – Dinesh Maurya Mar 29 '17 at 09:41
  • 1
    @DineshMaurya - The standard prohibited it because allowing it is more trouble than it's worth. Bit-fields have limited uses in C, and even fewer in C++. If you want named "bits" you can achieve it with a union of a fixed width integer and classes the encapsulate access to specific bits (as C++11 an onward allows). – StoryTeller - Unslander Monica Mar 29 '17 at 09:51
3

The reason the standard prohibits it is because static data members need to be instantiated somewhere - in your example, a compilation unit somewhere would need to contain:

int A::mem :10;

which is invalid, in the same way that a standalone non-member bitfield variable such as:

int foo :10;

is invalid.

Of course one can ask why this is prohibited, but that's a wider question not related to it being a class member.

Jeremy
  • 5,055
  • 1
  • 28
  • 44