Commit 7e348e5
committed
Fix unnecessary padding for basic_string's __short
Originally, padding in the struct `__short` was done with arrays, but when no padding was required, we got undefined behavior due to a zero-length array.
C++17 11.3.4 Arrays p1:
```
If the <<length of array>> is present, it shall be a converted constant
expression of type std::size_t and its value shall be greater than zero.
```
A fix was proposed in commit d95597d, which worked around the undefined behavior by wrapping the padding array in a struct if there was padding required and omitting the array altogether, with the help of a specialization, when the padding is zero.
This change doesn't just work around the problem but changes the semantics of the code, introducing an issue.
Contrary to C, in C++ empty structs and classes must have a size greater than zero.
C++ 17 12 Classes p4:
```
Complete objects and member subobjects of class type shall have nonzero size.
```
So whenever there is no padding required we insert an empty struct-sized padding, which is 1 byte on X86_64 (but we won't feel it there, since on that platform padding is required if I am correct).
I would like to propose another standard compliant solution using unnamed 0 width bitfields.
12.2.4 Bit-fields p2:
```
As a special case, an unnamed bit-field with a width of zero
specifies alignment of the next bit-field at an allocation unit boundary. Only when declaring an unnamed
bit-field may the value of the constant-expression be equal to zero.
```1 parent d3153ad commit 7e348e5
1 file changed
+2
-10
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
715 | 715 | | |
716 | 716 | | |
717 | 717 | | |
718 | | - | |
719 | | - | |
720 | | - | |
721 | | - | |
722 | | - | |
723 | | - | |
724 | | - | |
725 | | - | |
726 | 718 | | |
727 | 719 | | |
728 | 720 | | |
| |||
827 | 819 | | |
828 | 820 | | |
829 | 821 | | |
830 | | - | |
831 | 822 | | |
832 | 823 | | |
| 824 | + | |
833 | 825 | | |
834 | 826 | | |
835 | 827 | | |
| |||
879 | 871 | | |
880 | 872 | | |
881 | 873 | | |
882 | | - | |
| 874 | + | |
883 | 875 | | |
884 | 876 | | |
885 | 877 | | |
| |||
0 commit comments