Why bit fields are not allowed as static members of class data

Can someone explain the reason why bit fields are not allowed as static members of a class? For example, a class defined as:

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

not compiled.

Compiling this class with different compilers: -

1- g ++ throws an error: -

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

static int mem :10;

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

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 an error: -

'A :: mem' :: illegal storage class

'int A :: mem': republishing member function is not allowed

+4
source share
2 answers

, , ++ :

[class.bit] 12.2.4/3

. ([basic.fundamental]). bool . - , . ([dcl.init.ref]). [: const T & l, -, ; . . [Dcl.init.ref]. - ]

? , - - C. . , .

, , -, , , IMHO - .

+6

, , , - - - :

int A::mem :10;

, , , :

int foo :10;

.

, , , , .

+1

Source: https://habr.com/ru/post/1673460/


All Articles