0

Reading https://en.cppreference.com/w/cpp/language/bit_field, are the following conclusions correct?

  • whether adjacent bit-fields have no padding in between is implementation-defined (this reads different in https://eel.is/c++draft/class.bit#:bit-field)
  • the placement of a bit-field within the class-object is implementation-defined
  • the position of the bits inside a bit-field is implementation-defined (although C++20 defines signed-integer to be 2ths complement).

(for C see: Characteristics of bit-Fields in C)

wimalopaan
  • 4,838
  • 1
  • 21
  • 39
  • 1
    "Allocation of bit-fields within a class object is implementation-defined. Alignment of bit-fields is implementation-defined." does that not answer your quesitons? Why not? – 463035818_is_not_an_ai Jan 30 '23 at 12:11
  • Because I see differences to the C characteristics (especially the padding) and I see such definitions relying on no-padding (packed bit-fields) in many µC headers, that do not prevent using them from C++. – wimalopaan Jan 30 '23 at 12:14
  • 1
    "implementation defined" means it is defined and documented (!) by the implementation. It does not mean that any code that relies and specfics is automatically wrong. It only means that it is not necessarily portable – 463035818_is_not_an_ai Jan 30 '23 at 12:16
  • I know. But I see also nothing that prevents using the "wrong" compiler ... – wimalopaan Jan 30 '23 at 12:18
  • in principle it could even be that it is the same on every implementation, then the standard still says that it is implementation defined. (Afaik this is what happened to 2s complement, until it was realized that it can be specified in the standard) – 463035818_is_not_an_ai Jan 30 '23 at 12:18
  • It is known that, for example, VC++ and g++ start using bits at opposite ends of the underlying storage. Also, if the bits don't fit, like `unsigned x : 16; unsigned y : 17;` some compilers will start `y` in a separate storage unit, some will split it. – BoP Jan 30 '23 at 12:18
  • @463035818_is_not_a_number: can you confirm my above points? – wimalopaan Jan 30 '23 at 12:18
  • you say that you see disagreement between cppreference and the wording in the standard. I dont see that. Actually I just wanted to comment that I still think the question is too broad and could get some clarification (cf my first comment). I didn't want to start spamming again, sorry ;) – 463035818_is_not_an_ai Jan 30 '23 at 12:22
  • @463035818_is_not_a_number: the wiki says that packing is implementation-defined whereas the standard-doc says that bit-fields are packed. – wimalopaan Jan 30 '23 at 12:42
  • where does it say that? I am not saying that you are wrong. I am not saying that I am sure that I know the answer. I am just saying that you could add more information to the quesiton. If you think they disagree, then quote the relevant parts, and say why you think they do not match. This would make a clear specific question – 463035818_is_not_an_ai Jan 30 '23 at 12:44
  • wiki: "Multiple adjacent bit-fields are usually packed together (although this behavior is implementation-defined)" vs std: "Bit-fields are packed into some addressable allocation unit." – wimalopaan Jan 30 '23 at 12:47
  • @wimalopaan bit-fields - you __must__ read your implementation docs as everything important about them is _"implementation defined"_. If you want portable bit-fields, then don't use them. Instead use fix-width integer type(s) with your own masking and packing/unpacking. – – Richard Critten Jan 30 '23 at 12:47
  • @RichardCritten I know that. The question is just as a "language lawyer" – wimalopaan Jan 30 '23 at 12:48
  • 3
    [\[class.bit\]](https://eel.is/c++draft/class.bit#:bit-field,implementation-defined_alignment_of) _"[Note 1: Bit-fields straddle allocation units on some machines and not on others. Bit-fields are assigned right-to-left on some machines, left-to-right on others. — end note]"_. That is - if you want a portable fixed layout you can't use bit-fields. – Richard Critten Jan 30 '23 at 12:48
  • @RichardCritten That is no answer, if the are packed. – wimalopaan Jan 30 '23 at 12:51
  • 1
    The question has three very clear points towards one specific feature in one language. So it would be helpful to get one answer comprising all three points of the question. – wimalopaan Jan 30 '23 at 12:52

1 Answers1

2

"The question has three very clear points towards one specific feature in one language. So it would be helpful to get one answer comprising all three points of the question"

Addressing points one-by-one

In short, the conclusion is that no guarantees exist that bit-field implementation between various new specifications of C++ will be consistent. Portability is therefore difficult, if not impossible from one C++ implementation to the other, forcing that the specifications and other documentation supporting the C++ compiler being used must be consulted for any application using it to be sure of its implementation (rules) regarding how padding, or other attributes of bit-fields are implemented.

ryyker
  • 22,849
  • 3
  • 43
  • 87