Is it possible to use bit fields in C ++ classes?

In C structures, you can specify a different bit length than the default bit length for a type like this:

struct MyStruct{ int myVar : 1; //Size of myVar is 1 bit (so it can take values 0 or 1 int myOtherVar: 4; //Size of myOtherVar is 4 bits (so it can take values 0 to 15) } 

This is called bit fields.

My question is, is it possible to do this also in C ++ classes, for example:

 class MyClass{ public: //Some methods private: int m_myAttribute : 1; int m_myOtherAttribute : 4; } 

I searched the Internet for this, but all the examples I found from bit fields used structs, not classes.

I tested this code and compiled it just fine, but I would like to know if the size of the attributes was really the given size or the compiler just ignored the bit fields and used the standard size int .

+6
source share
3 answers

Yes a class can have bitfield elements. In C ++, there is no difference between class and a struct , with the exception of the default access level and the default inheritance type. They are called class types. If you can do something in a struct , then you can do the same in a class . Since the default access levels are different, they will look a little different, but you will get the same. For instance,

 struct foo { int m_myAttribute : 1; int m_myOtherAttribute : 4; int m_myOtherOtherAttribute : 27; }; 

coincides with

 class bar { public: int m_myAttribute : 1; int m_myOtherAttribute : 4; int m_myOtherOtherAttribute : 27; }; 

Note that we had to use public: in the class, because by default the members are private.

Now about the size of bit fields in C ++. [class.bit] / 1 has all the necessary information:

A constant expression must be an integral constant expression with a value greater than or equal to zero. The value of the expression of the integral constant may be greater than the number of bits in the object representation (3.9) of the type of bit fields; in such cases, the extra bits are used as padding bits and do not participate in the representation of the values ​​(3.9) of the bit field. The distribution of bit fields within a class object is determined by the implementation. The alignment of bit fields is determined by the implementation. Bit fields are packed into some address distribution unit. [Note: bit fields highlight allocation units on some machines, not others. Bit fields are assigned from right to left on some machines, from left to right on others. -end note]

my accent

From this we get that the size of the bit-feild will be at least the size of the underlying data type, but if you redistribute the space, then this additional space turns into a padding and is not used for the value of the bit member of the field.

+8
source

In C ++, it is perfectly legal to use bit fields with classes or structures. I also recommend this question for further immersion in the similarities and differences of the two: What are the differences between structure and class in C ++?

And yes, the bit size is really taken into account, just remember that the size of the memory layout depends on the implementation

 class test { int a : 3; void fun() { // std::cout << sizeof(a); // Illegal (*) a = 5; // Warning! Implicit truncation from int to 3-bit bitfield yields -3 (2 complement) } }; int main() { std::cout << sizeof(test); // ABI-dependent, might be 4 bytes } 

(*) Let me also touch on your comment regarding the use of sizeof and bit fields: he is not allowed to use sizeof on a glvalue denoting a bit field, as in [expr.sizeof]/p1

+2
source

As an implementation quality (QoI), yes, the size of the members is actually the specified size with all the compilers that I know of.

If you are asking whether the sizes MUST conform (according to the standard), you should ask yourself, what would you say? (In standard compliance.) If you cannot (and I don’t think you can), then according to the as-if rule, the compiler can do what it likes.

For more details see section 9.6 of C ++ 14 standard (actually n4296.)

0
source

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


All Articles